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 |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-28 : 23:22:05
|
My method of sum totalSELECT t.st_code, m.st_desc, m.unit, sum(t.qtt_out) as qtt_out, sum(t.quantity2) as quantity2, sum(t.qtt_out-t.quantity2) as b_orderqtyFROM ord_trx t left outer join st_mast m on m.st_code=t.st_code WHERE (t.trx_type='SO' or t.trx_type='SOX') and (t.do_date>='20070101' and t.do_date<'20080101') and (t.st_code>='' and t.st_code<='zzzzzzzz')GROUP BY t.st_code, m.st_desc, m.unitUNIONSELECT NULL, NULL, 'TOTAL', sum(t.qtt_out) as qtt_out, sum(t.quantity2) as quantity2, sum(t.qtt_out-t.quantity2) as b_orderqtyFROM ord_trx t left outer join st_mast m on m.st_code=t.st_code WHERE (t.trx_type='SO' or t.trx_type='SOX') and (t.do_date>='20070101' and t.do_date<'20080101') and (t.st_code>='' and t.st_code<='zzzzzzzz')ORDER BY t.st_code As you can see, i needed a union to find the total, is there any more efficient method to achieve the result?(Without using reporting software) |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-28 : 23:58:47
|
see http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=127637Alternatively, since your GROUP BY is only single column you can do this.use derived table and place the LEFT JOIN to st_mast outside and use WITH ROLLUP on the GROUP BYSELECT t.st_code, m.st_desc, unit = case when t.st_code is null then 'TOTAL' else m.unit end, t.qtt_out, t.quantity2, t.b_orderqtyFROM ( SELECT t.st_code, --m.st_desc, m.unit, sum(t.qtt_out) as qtt_out, sum(t.quantity2) as quantity2, sum(t.qtt_out-t.quantity2) as b_orderqty FROM ord_trx t WHERE (t.trx_type='SO' or t.trx_type='SOX') and (t.do_date>='20070101' and t.do_date<'20080101') and (t.st_code>='' and t.st_code<='zzzzzzzz') GROUP BY t.st_code --, m.st_desc, m.unit WITH ROLLUP ) t -- this is a derived table LEFT OUTER JOIN st_mast m on m.st_code = t.st_code ORDER BY t.st_code KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-29 : 00:04:43
|
| wow you still remember my post >.<""" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-29 : 00:06:47
|
quote: Originally posted by waterduck wow you still remember my post >.<"""
 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-06-29 : 00:07:06
|
| Eh why i din't notice that i can join them after group by!!btw thank alot to khtan ^^ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-29 : 00:19:43
|
That's call DERIVED TABLE KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|