TSFLLC
asked on
Select statement with multiple joins
How can I select fields from multiple select statements into one select statement. I have the following selects with a common field called contact_id. The select statements are greatly simplified for this purpose. Current, Zero30, etc are calculated and not fields in a table.
I want to be able to generate ONE record with each of the values from the joined selects. I've included a sample but don't know how the joins should be phrased (JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN).
SELECT a.contact_id, b.Current, c.Zero30, d.ThirtyOne60, e.SixtyOne90, f.Over90 FROM accounting a
(HOW TO JOIN EACH???)
(SELECT contact_id, Current FROM ar) AS b ON a.contact_id = b.contact_id JOIN?
(SELECT contact_id, Zero30 FROM ar) AS c ON a.contact_id = c.contact_id JOIN?
(SELECT contact_id, ThirtyOne60 FROM ar) AS d ON a.contact_id = d.contact_id JOIN?
(SELECT contact_id, SixtyOne90 FROM ar) AS e ON a.contact_id = e.contact_id JOIN?
(SELECT contact_id, Over90 FROM ar) AS f ON a.contact_id = f.contact_id
WHERE a.contact_id = 455
I want to be able to generate ONE record with each of the values from the joined selects. I've included a sample but don't know how the joins should be phrased (JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN).
SELECT a.contact_id, b.Current, c.Zero30, d.ThirtyOne60, e.SixtyOne90, f.Over90 FROM accounting a
(HOW TO JOIN EACH???)
(SELECT contact_id, Current FROM ar) AS b ON a.contact_id = b.contact_id JOIN?
(SELECT contact_id, Zero30 FROM ar) AS c ON a.contact_id = c.contact_id JOIN?
(SELECT contact_id, ThirtyOne60 FROM ar) AS d ON a.contact_id = d.contact_id JOIN?
(SELECT contact_id, SixtyOne90 FROM ar) AS e ON a.contact_id = e.contact_id JOIN?
(SELECT contact_id, Over90 FROM ar) AS f ON a.contact_id = f.contact_id
WHERE a.contact_id = 455
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I guess my mind had gotten a little fuzzy. Actually 'accounting' is suppose to be 'ar'. We're working with only ONE table. I said in my initial post that I simplified the separate select statements of b,c,d,e and f. They each have nested select statements where they pull records and the balance is a grouped field, hence Current, Zero30...etc.
In using INNER JOIN what if no record meets the criteria set forth in one of the select statements? Do the contact_id, Current values come back Null or would it cause the Select a.contact_id, b.Current, c.Zero30......to come back with no record?
I have to have it come back Null else a valuable A/R record would not be included in the select.
In using INNER JOIN what if no record meets the criteria set forth in one of the select statements? Do the contact_id, Current values come back Null or would it cause the Select a.contact_id, b.Current, c.Zero30......to come back with no record?
I have to have it come back Null else a valuable A/R record would not be included in the select.
I strongly suspect you can calculate all the totals at once, rather than in several different queries and then joining them.
You probably have the Current|0-30|31-60|... in WHERE clauses for each statement, with the rest of the query -- joins, etc. -- the same. Instead, you can do this:
SELECT contact_id,
SUM(CASE WHEN ? <= 0 THEN amt ELSE 0 END) AS Current,
SUM(CASE WHEN ? BETWEEN 1 AND 30 THEN amt ELSE 0 END) AS Zero30,
SUM(CASE WHEN ? BETWEEN 31 AND 60 THEN amt ELSE 0 END) AS Thirtyone60,
...
FROM tableName
WHERE ...other conditions excluding # of days...
GROUP BY contact_id
You probably have the Current|0-30|31-60|... in WHERE clauses for each statement, with the rest of the query -- joins, etc. -- the same. Instead, you can do this:
SELECT contact_id,
SUM(CASE WHEN ? <= 0 THEN amt ELSE 0 END) AS Current,
SUM(CASE WHEN ? BETWEEN 1 AND 30 THEN amt ELSE 0 END) AS Zero30,
SUM(CASE WHEN ? BETWEEN 31 AND 60 THEN amt ELSE 0 END) AS Thirtyone60,
...
FROM tableName
WHERE ...other conditions excluding # of days...
GROUP BY contact_id
ASKER
Thanks for the reply Scott.
I think I'm going to be sick. I think I just realized something about SQL that I never knew and I truly feel nauseous. This shows that I have alot to learn.
I have always been using the word HAVING .... after my GROUP BY for the conditional word instead of using WHERE before the GROUP BY.
Because I have always been using HAVING... it has required me to create nested select statements I would not have ordinarily had to create? For example the records below would yield two different results depending on the two subsequent SQL statements.
ContactID DueDate InvoiceAmt
1 12/20/2006 150.00
1 12/22/2006 150.00
1 12/24/2006 150.00
SELECT ContactID, SUM(InvoiceAmt) AS InvAmt FROM file WHERE DueDate >= CONVERT(datetime,'12/20/20 06') GROUP BY ContactID
Results would be (which is what I want):
ContactID InvAmt
1 450.00
Whereas the SQL statement
SELECT ContactID, SUM(InvoiceAmt) AS InvAmt FROM file WHERE DueDate >= CONVERT(datetime,'12/20/20 06') GROUP BY ContactID
would return an error about DueDate not being part of the aggregate grouping... and I would have to do create an idiotic nested select like this.
SELECT a.ContactID, SUM(a.InvoiceAmt) AS InvAmt FROM (SELECT ContactID, InvoiceAmt FROM file WHERE DueDate >= CONVERT(datetime,'12/20/20 06')) a GROUP BY a.ContactID
If I'm on the right track....
Is it safe to say that I can SUM on multiple fields and at the same time include any number of conditions through using WHERE without having to include those conditional fields as output?
I think I'm going to be sick. I think I just realized something about SQL that I never knew and I truly feel nauseous. This shows that I have alot to learn.
I have always been using the word HAVING .... after my GROUP BY for the conditional word instead of using WHERE before the GROUP BY.
Because I have always been using HAVING... it has required me to create nested select statements I would not have ordinarily had to create? For example the records below would yield two different results depending on the two subsequent SQL statements.
ContactID DueDate InvoiceAmt
1 12/20/2006 150.00
1 12/22/2006 150.00
1 12/24/2006 150.00
SELECT ContactID, SUM(InvoiceAmt) AS InvAmt FROM file WHERE DueDate >= CONVERT(datetime,'12/20/20
Results would be (which is what I want):
ContactID InvAmt
1 450.00
Whereas the SQL statement
SELECT ContactID, SUM(InvoiceAmt) AS InvAmt FROM file WHERE DueDate >= CONVERT(datetime,'12/20/20
would return an error about DueDate not being part of the aggregate grouping... and I would have to do create an idiotic nested select like this.
SELECT a.ContactID, SUM(a.InvoiceAmt) AS InvAmt FROM (SELECT ContactID, InvoiceAmt FROM file WHERE DueDate >= CONVERT(datetime,'12/20/20
If I'm on the right track....
Is it safe to say that I can SUM on multiple fields and at the same time include any number of conditions through using WHERE without having to include those conditional fields as output?
ASKER
I screwed up. I copied and pasted without making the change to the following:
Whereas the SQL statement
SELECT ContactID, SUM(InvoiceAmt) AS InvAmt FROM file GROUP BY ContactID HAVING DueDate >= CONVERT(datetime,'12/20/20 06')
would return an error about DueDate not being part of the aggregate grouping... and I would have to do create an idiotic nested select like this.
SELECT a.ContactID, SUM(a.InvoiceAmt) AS InvAmt FROM (SELECT ContactID, InvoiceAmt FROM file WHERE DueDate >= CONVERT(datetime,'12/20/20 06')) a GROUP BY a.ContactID
Whereas the SQL statement
SELECT ContactID, SUM(InvoiceAmt) AS InvAmt FROM file GROUP BY ContactID HAVING DueDate >= CONVERT(datetime,'12/20/20
would return an error about DueDate not being part of the aggregate grouping... and I would have to do create an idiotic nested select like this.
SELECT a.ContactID, SUM(a.InvoiceAmt) AS InvAmt FROM (SELECT ContactID, InvoiceAmt FROM file WHERE DueDate >= CONVERT(datetime,'12/20/20
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I understand what you're saying. As I was waiting (hoping) for another quick post from you, I modified my original select with WHERE making it more efficient. I have one other issue that I will have to deal with but this has helped immensely.
Now I get to re-review other SQL statements where I use HAVING and not WHERE. All part of the learning curve.
dbeneit understood and helped me out with the original issue but your postings had value I wasn't expecting but welcome. Split points.
Thanks all !!
Sincerely,
Phil
Now I get to re-review other SQL statements where I use HAVING and not WHERE. All part of the learning curve.
dbeneit understood and helped me out with the original issue but your postings had value I wasn't expecting but welcome. Split points.
Thanks all !!
Sincerely,
Phil
SELECT a.contact_id, b.Current, b.Zero30, b.ThirtyOne60, b.SixtyOne90, b.Over90 FROM accounting a
left join ar as b on ON a.contact_id = b.contact_id
mmm... doesnt make sense, cause in that case you wouldnt need table at all...
so, please describe how many tables you have and which fields are in which table