Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2009-09-29 : 10:02:12
|
i have table:Month Year Amount Customer1 2009 100 X1 2009 200 X2 2009 100 X2 2009 200 X3 2009 100 X3 2009 200 X 1 2009 200 Y2 2009 200 Y3 2009 200 Yhow can i write query and see this result: Month 1 2 3Customer X 100 100 100 Customer X 200 200 200 TOTAL x 300 300 300 Customer Y 200 200 200 TOTAL Y 300 300 300 TOTATL ALL 400 400 400 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-29 : 10:19:22
|
| It should be done in front end applicationOtherwise read about ROLLUP or CUBE operator in sql server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2009-09-29 : 10:40:45
|
| we couldnt success do it in end application |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-30 : 02:03:51
|
quote: Originally posted by inbs we couldnt success do it in end application
What do you mena by this?MadhivananFailing to plan is Planning to fail |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-09-30 : 02:09:36
|
eeeeeeee how come month and year separated... Hope can help...but advise to wait pros with confirmation... |
 |
|
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2009-09-30 : 02:24:50
|
i change my questioni have this tableYear Month Customer Amount2009 1 A1 1 2009 1 A2 2 2009 1 A3 3 2009 1 B1 4 2009 1 B2 52009 1 B3 6 i need this result (by month and year)SubTotal By A SubTotal By BTOTAl A+BYear Month Customer Amount SUBTOTAL TOTAL2009 1 A1 1 6 212009 1 A2 2 6 212009 1 A3 3 6 212009 1 B1 4 15 212009 1 B2 5 15 21 2009 1 B3 6 15 21 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-30 : 02:25:43
|
quote: Originally posted by inbs my question is how i make subtotal for x and for y
Did you read my first reply?MadhivananFailing to plan is Planning to fail |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-09-30 : 02:35:59
|
madhi...how u do 1 2 3Customer X 100 100 100 Customer X 200 200 200 Hope can help...but advise to wait pros with confirmation... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-30 : 02:54:03
|
to your original questionDECLARE @sample TABLE( [MONTH] int, [YEAR] int, Amount int, Customer varchar(10))INSERT INTO @sampleSELECT 1, 2009, 100, 'X' UNION ALLSELECT 1, 2009, 200, 'X' UNION ALLSELECT 2, 2009, 100, 'X' UNION ALLSELECT 2, 2009, 200, 'X' UNION ALLSELECT 3, 2009, 100, 'X' UNION ALLSELECT 3, 2009, 200, 'X' UNION ALLSELECT 1, 2009, 200, 'Y' UNION ALLSELECT 2, 2009, 200, 'Y' UNION ALLSELECT 3, 2009, 200, 'Y'SELECT Customer, [YEAR], [1], [2], [3]FROM ( SELECT Customer, [YEAR], [MONTH], Amount, dense_rank = row_number() OVER (PARTITION BY Customer, [MONTH] ORDER BY [YEAR]) FROM @sample UNION ALL SELECT Customer + ' TOTAL', [YEAR], [MONTH], Amount = SUM(Amount), dense_rank = 1 FROM @sample GROUP BY Customer, [YEAR], [MONTH] UNION ALL SELECT 'TOTAL ALL', [YEAR], [MONTH], Amount = SUM(Amount), dense_rank = 1 FROM @sample GROUP BY Customer, [YEAR], [MONTH] ) d pivot ( SUM(Amount) FOR [MONTH] IN ([1], [2], [3]) ) pORDER BY CASE WHEN Customer = 'TOTAL ALL' THEN 2 ELSE 1 END, Customer/*Customer YEAR 1 2 3 ---------------- ----------- ----------- ----------- ----------- X 2009 100 100 100X 2009 200 200 200X TOTAL 2009 300 300 300Y 2009 200 200 200Y TOTAL 2009 200 200 200TOTAL ALL 2009 500 500 500(6 row(s) affected)*/ KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-30 : 02:54:18
|
to your revised questionDECLARE @sample TABLE( [YEAR] int, [MONTH] int, Customer varchar(10), Amount int)INSERT INTO @sampleSELECT 2009, 1, 'A', 1 UNION ALLSELECT 2009, 1, 'A', 2 UNION ALLSELECT 2009, 1, 'A', 3 UNION ALLSELECT 2009, 1, 'B', 4 UNION ALLSELECT 2009, 1, 'B', 5 UNION ALLSELECT 2009, 1, 'B', 6SELECT s.[YEAR], s.[MONTH], s.Customer, s.Amount, SubTotal = SUM(Amount) OVER (PARTITION BY s.Customer), Total = SUM(Amount) OVER ()FROM @sample s/*YEAR MONTH Customer Amount SubTotal Total ----------- ----------- ---------- ----------- ----------- ----------- 2009 1 A 1 6 212009 1 A 2 6 212009 1 A 3 6 212009 1 B 4 15 212009 1 B 5 15 212009 1 B 6 15 21(6 row(s) affected)*/ KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-09-30 : 02:55:14
|
[code]SELECT yearly, monthly, customer, amount, (SELECT SUM(amount) FROM @fun f1 WHERE f1.customer=f.customer)subtotal, totalFROM @fun fCROSS APPLY(SELECT SUM(amount) partition by customer, SUM(amount) totalFROM @fun)f2ORDER BY CUSTOMER[/code] Hope can help...but advise to wait pros with confirmation... |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-09-30 : 02:56:11
|
.... sifu =.= Hope can help...but advise to wait pros with confirmation... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-30 : 02:56:11
|
i assumed your customer should be A and B and not A1, A2 etc else the SUBTOTAL and TOTAL does not make sense KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-30 : 02:58:44
|
quote: Originally posted by waterduck .... sifu =.= Hope can help...but advise to wait pros with confirmation...
sorry beat you to it  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-30 : 03:01:28
|
you can also use 2 CROSS APPLY to do thatSELECT s.[YEAR], s.[MONTH], s.Customer, s.Amount, SubTotal, TotalFROM @sample s CROSS APPLY ( SELECT SubTotal = SUM(Amount) FROM @sample x WHERE x.Customer = s.Customer ) st CROSS APPLY ( SELECT Total = SUM(Amount) FROM @sample x ) t KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-09-30 : 03:04:04
|
sifu..where to find the cute icon!!! Hope can help...but advise to wait pros with confirmation... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-30 : 03:06:09
|
click on "Reply with Quote" and you will be able to see that KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2009-09-30 : 03:49:27
|
i success with my way,but it run a long timehow can i optimize it?SELECT v.Customer,v.Month,v.Year,v.OAmount,v.TarAmount,--SubTotalOAmountCASE WHEN v.Customer LIKE '%A%' THEN (SELECT SUM(Amount) FROM TABLE_V WHERE Month=v.Month AND Year=v.Year AND Customer LIKE '%A%') WHEN v.Customer LIKE '%B%' THEN (SELECT SUM(OAmount) FROM TABLE_V WHERE Month=v.Month AND Year=v.Year AND Customer LIKE '%B%')ELSE OAmount END AS SubTotalOAmount,--SubTotalTarAmountCASE WHEN v.Customer LIKE '%A%' THEN (SELECT SUM(TarAmount) FROM TABLE_V WHERE Month=v.Month AND Year=v.Year AND Customer LIKE '%A%') WHEN v.Customer LIKE '%B%' THEN (SELECT SUM(TarAmount) FROM TABLE_V WHERE Month=v.Month AND Year=v.Year AND Customer LIKE '%B%')ELSE TarAmount END AS SubTotalTarAmountFROM TABLE_V vGROUP BY v.Customer,v.Month,v.Year,v.OAmount,v.TarAmount |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
|
|
|
|
|