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
TSFLLCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dreadyCommented:
I am not really sure wether i understand what you want to do, but i asume there are 2 tables, accounting and ar? If not, please name the table names in the other select statements.

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
0
dbeneitCommented:
I supposed that each "ar" table is a distinct table or joins of tables.
if each contact_id is in alls tables INNER JOIN, if there are records in accounting that not exist in others LEFT JOIN , and right JOIN is if you have records in  "f" and they can't be in others.

SELECT a.contact_id, b.Current, c.Zero30, d.ThirtyOne60, e.SixtyOne90, f.Over90 FROM accounting a
INNER JOIN
(SELECT contact_id, Current FROM ar) AS b ON a.contact_id = b.contact_id
INNER JOIN
(SELECT contact_id, Zero30 FROM ar) AS c ON a.contact_id = c.contact_id
INNER JOIN
(SELECT contact_id, ThirtyOne60 FROM ar) AS d ON a.contact_id = d.contact_id
INNER JOIN
(SELECT contact_id, SixtyOne90 FROM ar) AS e ON a.contact_id = e.contact_id
INNER JOIN
(SELECT contact_id, Over90 FROM ar) AS f ON a.contact_id = f.contact_id
WHERE a.contact_id = 455

If ar is a unique table. ANd all record of accounting  have 1 or more records in "ar"  then better inner join than left join

0
TSFLLCAuthor Commented:
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.
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Scott PletcherSenior DBACommented:
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
0
TSFLLCAuthor Commented:
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/2006') 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/2006') 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/2006')) 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?
0
TSFLLCAuthor Commented:
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/2006')
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/2006')) a GROUP BY a.ContactID
0
Scott PletcherSenior DBACommented:
You should always use WHERE for conditions that apply to *each row individually*.  Use HAVING only for conditions that apply to *groups of rows*.

>> 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? <<
Yes, I think that is true, if I understand the q correctly.

Note, though, that I am also talking about using CASE statements to generate multiple, different totals from the same SELECT.  For example, typically aging is based on due_date.  So, to calc Current|1-30|31-60|61-90|..., the obvious way to total these is with individual SELECTs, like so:

SELECT id, SUM(amt) AS Current
FROM tableName
...joins...
WHERE due_date >= CONVERT(CHAR(8), GETDATE(), 112)  --today only
...other conditions...
--UNION ALL
SELECT id, SUM(amt) AS OneTo30
FROM tableName
...joins...
WHERE due_date >= DATEADD(DAY, -30, CONVERT(CHAR(8), GETDATE(), 112)) AND
    due_date < CONVERT(CHAR(8), GETDATE(), 112) --1 to 30 days old
...other conditions...
--UNION ALL
SELECT id, SUM(amt) AS ThirtyOneTo60
FROM tableName
...joins...
WHERE due_date >= DATEADD(DAY, -60, CONVERT(CHAR(8), GETDATE(), 112)) AND
    due_date < DATEADD(DAY, -30, CONVERT(CHAR(8), GETDATE(), 112)) --31 to 60 days old
...other conditions...
...other UNIONS and SELECTs...

However, instead of that, you can compute all the aging SUMs in one query like so:
SELECT id,
    SUM(CASE WHEN due_date >= CONVERT(CHAR(8), GETDATE(), 112) THEN amt ELSE 0 END) AS Current,
    SUM(CASE WHEN due_date >= DATEADD(DAY, -30, CONVERT(CHAR(8), GETDATE(), 112)) AND
    due_date < CONVERT(CHAR(8), GETDATE(), 112) THEN amt ELSE 0 END) AS OneTo30,
    SUM(CASE WHEN due_date >= DATEADD(DAY, -60, CONVERT(CHAR(8), GETDATE(), 112)) AND due_date < DATEADD(DAY, -30, CONVERT(CHAR(8), GETDATE(), 112)) THEN amt ELSE 0 END) AS ThirtyOneTo60,
    SUM(...), ...
FROM tableName
...joins...
WHERE ...other conditions...
GROUP BY id
ORDER BY id
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TSFLLCAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.