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
444 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
ID: 22775770
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
ID: 31508711
You're great! Thank you very much!!!
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 setup several different housekeeping processes for a SQL Server.

785 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