Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Complex Query using Self-Joins


Complex Query using Self-Joins

Author
Message
Steve DiDomenico-197994
Steve DiDomenico-197994
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 5
As an example, is it possible write SQL to take these 8 rows and return the following 2 rows? My self-joins are too simple and I'm getting a cartesian effect and can't figure this out.

INPUT

close_total received_tag monetary_type_id
------------- ------------- ----------------
01.00 20131212 1
02.00 20131212 2
10.00 20131213 1
20.00 20131213 1
30.00 20131213 2
40.00 20131213 2
50.00 20131213 3
60.00 20131213 3

OUTPUT

received_tag sum_close_total_all sum_close_total_1 sum_close_total_2 sum_close_total_3
--------------------------------------------------------------------------------------------------
20131212 3.00 1.00 2.00 0.00
20131213 210.00 30.00 70.00 110.00

NOTES

sum_close_total_all - monetary_type_id = ALL
sum_close_total_1 - monetary_type_id = 1
sum_close_total_2 - monetary_type_id = 2
sum_close_total_3 - monetary_type_id = 3

Thanks,

Steve
LinksUp
LinksUp
Right there with Babe
Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)

Group: General Forum Members
Points: 736 Visits: 4005
Try this:


SELECT tag_date
, SUM(close_total)
, ISNULL(SUM(CASE id WHEN 1 THEN close_total ELSE 0 END), 0) AS col1
, ISNULL(SUM(CASE id WHEN 2 THEN close_total ELSE 0 END), 0) AS col2
, ISNULL(SUM(CASE id WHEN 3 THEN close_total ELSE 0 END), 0) AS col3
FROM @t1
GROUP BY tag_date



__________________________________________________________________________________________________________
How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/
Steve DiDomenico-197994
Steve DiDomenico-197994
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 5
Wow, perfect. Thanks. Happy Holidays.
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