SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


QUERY SALVATION


QUERY SALVATION

Author
Message
davdam8
davdam8
Say Hey Kid
Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)

Group: General Forum Members
Points: 670 Visits: 668
Hi geniuses, I need you guys to fix me a query.
Here's my table


ReceiverGroup PayerGroup Value
GroupA GroupA 1000
GroupA GroupC 3000
GroupA GroupC 6000
GroupB GroupD 2500
GroupB GroupA 5000
GroupB GroupC 2500
GroupE GroupA 1500
GroupE GroupB 3500



Here's the query we need to fix:

SELECT DISTINCT Group, Received, Paid
FROM (
SELECT a.ReceiverGroup AS Group, SUM(a.Value) as Received, 0 as Paid
FROM Test a
Group by a.ReceiverGroup
UNION ALL
SELECT b.PayerGroup as Group, 0 as Received, SUM(b.Value) as Paid
FROM Test b
Group by b.PayerGroup
) AS c




Which gives me:


Group Received Paid
GroupA 0 7500
GroupA 10000 0
GroupB 0 3500
GroupB 10000 0
GroupC 0 11500
GroupD 0 2500
GroupE 5000 0



What I want:


Group Received Paid
GroupA 10000 7500
GroupB 10000 3500
GroupC 0 11500
GroupD 0 2500
GroupE 5000 0


Thinknook
Thinknook
Mr or Mrs. 500
Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)

Group: General Forum Members
Points: 569 Visits: 164990
Just building on what you already have...

SELECT Group, SUM(Received), SUM(Paid)
FROM (
SELECT a.ReceiverGroup AS Group, SUM(a.Value) as Received, 0 as Paid
FROM Test a
Group by a.ReceiverGroup
UNION ALL
SELECT b.PayerGroup as Group, 0 as Received, SUM(b.Value) as Paid
FROM Test b
Group by b.PayerGroup
) AS c
GROUP BY Group




Assuming SUM is the correct aggregation for Received and Paid

-----------------
... Then again, I could be totally wrong! Check the answer.
Check out posting guidelines here for faster more precise answers.

I believe in Codd
... and Thinknook is my Chamber of Understanding
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search