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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

696 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