Solved

How would you select the same value from the same table multiple times with different filters/where statements for each?

Posted on 2008-10-22
2
442 Views
Last Modified: 2010-04-21
I need to make a select statement to do the following:
Select
D.Year
Sum of D.C_REFUND as [Exp_Ref]
Sum of R.AMT_REC as [RECEIPTS],
Sum of R.AMT_REC as [ADJUSTMENT] where R.Type = 'Adjustment' ,
Sum of R.AMT_REC as [CLIENT.REC] where R.Type = 'Client',
Sum of D.C_Short,
From D
Group by Year
Order Year by most recent on top

How would I go about creating the select statement and how would I be able to make sure that if for example, there is nothing in the 'R' table that it would still show records for everything that is in the 'D' table and if there is nothing in the D table I still want the R table to show if there are values to be shown.

I do not know how to go about this.
0
Comment
Question by:JohnnyBCJ
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
Comment Utility
I am guessing on what field to join the two tables on...


SELECT d.[Year], SUM(COALESCE(D.C_REFUND, 0)) AS Exp_Ref,
    SUM(COALESCE(R.AMT_REC, 0)) AS RECEIPTS,
    SUM(CASE WHEN R.[Type] = 'Adjustment' THEN COALESCE(R.AMT_REC, 0) ELSE 0 END) AS ADJUSTMENT,
    SUM(CASE WHEN R.[Type] = 'Client' THEN COALESCE(R.AMT_REC, 0) ELSE 0 END) AS [CLIENT.REC],
    SUM(D.C_SHORT) AS C_Short
FROM D Left JOIN
    R ON D.ID = R.ID
GROUP BY d.[Year]
ORDER BY d.[Year]
0
 

Author Closing Comment

by:JohnnyBCJ
Comment Utility
You're great! Thank you very much!!!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now