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
445 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

839 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