473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

is this possible with selects and joins?

hello

we have 3 tables

bugs
bugid | bugname

custom_field_value
bugid | fieldid | fieldvalue

custom_fields
fieldid | fieldname

now i wanne generate a result like this:

bugid | bugname | fieldname(1) | fieldname(2) | fieldname(3) | etc
1 | testbug | fieldvalue | fieldvalue | fieldvalue | etc

thanks for any help on this.
-gnog
Jul 23 '05 #1
1 1218
gnog wrote:
now i wanne generate a result like this:

bugid | bugname | fieldname(1) | fieldname(2) | fieldname(3) | etc
1 | testbug | fieldvalue | fieldvalue | fieldvalue | etc


I don't think this is practical to do in the format you describe in the
general case.

To do this, you'd need to join the bugs table to the custom_fields table
once for each field, and I assume since they are custom fields that
there is potentially an unbounded number of them. Also, you don't
specify that every bugid has the same set of custom fields (one might
expect this to be the case, but I'm not assuming it based on your schema).

You'd need an extra outer join for _each_ custom fieldvalue, and another
join for the matching fieldname. You'd also need a way to ensure that
you don't get repeated fields from the subsequent joins.

SQL doesn't support multi-way joins when you can't predict how many
terms you have, or when you have a variable number of terms per bugid in
this case.

SQL also doesn't support dynamic column aliases, so you wouldn't be able
to use "AS" to label the v1.fieldvalue columns based on the strings
found in f1.fieldname.

The only suggestion I have is that you first query all the distinct
custom field id's, and then based on the result, write code in your
application to build a query specific to your set of custom fields.

SELECT b.bugid, b.bugname,
v1.fieldvalue AS 'foo',
v2.fieldvalue AS 'bar'
FROM bugs AS b
LEFT OUTER JOIN custom_field_value AS v1
ON (b.bugid = v1.bugid AND v1.fieldid = 123)
INNER JOIN custom_fields AS f1
on v1.fieldid = f1.fieldid;
LEFT OUTER JOIN custom_field_value AS v2
ON (b.bugid = v2.bugid AND v2.fieldid = 456)
INNER JOIN custom_fields AS f2
on v2.fieldid = f2.fieldid
....etc.

But note that there's a hard limit to the number of tables joined. I
think it's 32 on typical systems. So you can support a maximum of 15
custom fields using this method, since you also need to join with the
bugs table. In any case, it's not very scalable.

The way I would do this would be to fetch the data in rows:

SELECT b.bugid, b.bugname, f1.fieldname, v1.fieldvalue
FROM bugs AS b
LEFT OUTER JOIN custom_field_value AS v1
ON b.bugid = v1.bugid
INNER JOIN custom_fields AS f1
on v1.fieldid = f1.fieldid;

Then I'd write code in my application to filter through the result set
and reformat the data into a tabular arrangement before output.

Regards,
Bill K.
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chris Greening | last post by:
I'm seeing a very strange problem with outer joins. The example below replicates the problem: create table data1 (dim1 integer, stat1 float); create table data2 (dim1 integer, stat2 float); ...
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
0
by: DG | last post by:
Can all "joins" be mimicked via nested selects?
3
by: Ian Boyd | last post by:
i know nothing about DB2, but i'm sure this must be possible. i'm trying to get a client to create a view (which it turns out is called a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but...
3
by: deko | last post by:
From what I understand, an Inner Join narrow the rows selected to the table with the least results... and an Outer Join does the opposite... SELECT qryTxToQ1.Q1Total, qryTxToQ2.Q2Total,...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
36
by: TC | last post by:
I've used Access for many years. Several times, I've encountered a bug which I refer to as the "Vanishing Joins" bug. When it happens, joins vanish randomly from queries. More specifically, all...
3
by: stoogots2 | last post by:
I have to either Join 4 tables, or retrieve one column from 3 tables, and one record from another. I can use a stored procedure to do the JOINs (doing that now), but it seems like overkill if I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.