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
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

726 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