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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 How to make Union join coming in order?

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 phonebill
where [prod type] ='Dedicated Outbound Voice' and [Place To St] ='MN'
Union
select 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 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')

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-06-29 : 17:17:55
[code]

SELECT minutes, seconds, totalAmt
FROM
(
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')
) t
ORDER BY SortOrder

[/code]

Tara
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2005-06-29 : 17:32:07
Great! Many thanks!
Go to Top of Page
   

- Advertisement -