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 |
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2005-06-29 : 17:07:55
|
| I have following union query. It gave me the right data, but the order is off. I want first select result to be in the first line, and the last to be the last. Even I rearranged the order of the selects, the order of the rows is still not controlable.Why and more importantly how to control? Am I missing something obvious?Thanks!select sum([Min]) as minutes, sum(Sec)as seconds, sum(amount) totalAmt from phonebillwhere [prod type] ='Dedicated Outbound Voice' and [Place To St] ='MN'Unionselect sum([Min]) as minutes, sum(Sec)as seconds, sum(amount) totalAmt from phonebillwhere [prod type] ='Dedicated Outbound Voice' and [call ctgy]='Domestic' and [Place To St] <>'MN'Unionselect sum([Min]) as minutes, sum(Sec)as seconds, sum(amount) totalAmt from phonebillwhere [prod type] ='Dedicated Outbound Voice' and ([call ctgy]='International' or [call ctgy]='Canada') |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-06-29 : 17:17:55
|
| [code]SELECT minutes, seconds, totalAmtFROM( select 1 AS SortOrder, sum([Min]) as minutes, sum(Sec)as seconds, sum(amount) totalAmt from phonebill where [prod type] ='Dedicated Outbound Voice' and [Place To St] ='MN' Union select 2, sum([Min]) as minutes, sum(Sec)as seconds, sum(amount) totalAmt from phonebill where [prod type] ='Dedicated Outbound Voice' and [call ctgy]='Domestic' and [Place To St] <>'MN' Union select 3, sum([Min]) as minutes, sum(Sec)as seconds, sum(amount) totalAmt from phonebill where [prod type] ='Dedicated Outbound Voice' and ([call ctgy]='International' or [call ctgy]='Canada')) tORDER BY SortOrder[/code]Tara |
 |
|
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2005-06-29 : 17:32:07
|
| Great! Many thanks! |
 |
|
|
|
|
|
|
|