[thelist] SQL Union and order by problem

XtiaN indotekken at yahoo.com
Thu Oct 18 04:26:15 CDT 2007


Hi,
can anyone help me ?

I've made some sql query like this (SQL Server 2000):

select * From (
SELECT top 100 PERCENT a,b,c    
FROM tb
WHERE DATEDIFF(day, b ,getdate())>=3 
order by b desc) asd 
union all
select * From (
SELECT top 100 PERCENT a,b,c    
FROM tb
WHERE DATEDIFF(day, b ,getdate())<3 
order by b asc) def


explanation:
i want to make query from union of 2 subqueries.
subquery 1 : simple query with order by b desc
subquery 2 : simple query with order by b asc
btw, a =  primary key (index)

But the result is not like i'd expected.. 
well, the result make all datetime data in column b order by asc... (not by desc first for subquery1, and followed by asc from subquery2)


ps: i added 'top 100 percent' because if i don't this message error will come ->
The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.

thanks in advance

 __________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the thelist mailing list