Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Below is my query, which returns top 5 rows order by date

SQL
select top 5 sd.sl_sub as topic,em.name as CreatedBy,sd.approved_date,'topic'as type  ,'' as comment_desc ,sd.sl_type
from wockhardt_022.sl_details sd
inner join dbo.employee_mapping map on map.user_id=sd.created_by  and effective_to_date is null
inner join  employee_master em   on map.employee_id=em.employee_id
where sd.div_code=@div_code
and sd.approved_flag=1
and sd.created_date < @last_date
--and approved_date > getdate() - 10

union

select top 5 sd.sl_sub as topic,em.name as CreatedBy,sc.approve_date as approved_date,'comment' as type ,sc.comment_desc,sd.sl_type
from wockhardt_022.sl_comment sc
inner join sl_details sd on sd.sl_id=sc.sl_id
inner join dbo.employee_mapping map on map.user_id=sc.comment_by and effective_to_date is null
inner join dbo.employee_master em on em.employee_id=map.employee_id
where sd.div_code=@div_code
and sc.approve_flag=1
and sc.approve_date < @last_date
order by approved_date  desc




But above query doesnt give output order by date.
What is wrong in this query?
Posted

Let's call ordering a function over a set: O(S). Let your two query results be A and B. Now, your union statement is C=A U O(B), and that won't be ordered. What you need is C=O(A U B): first union than sort.
SQL
SELECT * FROM
(
SELECT ... --first query
UNION
SELECT ... --second query
)
ORDER BY whatever
 
Share this answer
 
Comments
Aarti Meswania 27-Apr-13 2:41am    
5+! :)
Sweetynewb 27-Apr-13 2:51am    
getting error-Incorrect syntax near the keyword 'order'.
Zoltán Zörgő 27-Apr-13 2:55am    
I hope you have not used "whatever"! Karthik Harve gave to you the exact statement, I tried to give you the theory and the method... but as I see, you didn't get it... :(
Sweetynewb 27-Apr-13 3:03am    
after applying alias it is running but still one comment is not displaying. If i run separately then it gives that row but after union not.
I will try for this. thank you
Zoltán Zörgő 27-Apr-13 3:18am    
Interesting. If all column types are compatible and there is no duplication, you should get all. Just an idea: where you select a constant, or anything that's type or length is not always obvious, use CAST.
Hi,

The solution is very simple. In your first select statement, you are not ordering the based on the date, but in the Second query you are ordering by date, but when union the queries, you could not find the ordered rows. I suggest you to put order statement for the overall result set rather ordering individual queries.

try like below.
SQL
select A.topic, A.CreatedBy, A.approved_date, A.type, A.comment_desc, A.sl_type
from
(
select top 5 sd.sl_sub as topic,em.name as CreatedBy,sd.approved_date,'topic'as type  ,'' as comment_desc ,sd.sl_type
from wockhardt_022.sl_details sd
inner join dbo.employee_mapping map on map.user_id=sd.created_by  and effective_to_date is null
inner join  employee_master em   on map.employee_id=em.employee_id
where sd.div_code=@div_code
and sd.approved_flag=1
and sd.created_date < @last_date
--and approved_date > getdate() - 10

union
 
select top 5 sd.sl_sub as topic,em.name as CreatedBy,sc.approve_date as approved_date,'comment' as type ,sc.comment_desc,sd.sl_type
from wockhardt_022.sl_comment sc
inner join sl_details sd on sd.sl_id=sc.sl_id
inner join dbo.employee_mapping map on map.user_id=sc.comment_by and effective_to_date is null
inner join dbo.employee_master em on em.employee_id=map.employee_id
where sd.div_code=@div_code
and sc.approve_flag=1
and sc.approve_date < @last_date
) as A
order by A.approved_date desc   -- ordering by date to the resultant record set


hope it helps.
 
Share this answer
 
v2
Comments
Aarti Meswania 27-Apr-13 2:41am    
5+! :)
Karthik Harve 27-Apr-13 2:43am    
Thanks Aarti !!
Aarti Meswania 27-Apr-13 2:44am    
welcome.
Sweetynewb 27-Apr-13 2:53am    
same error-Incorrect syntax near the keyword 'order'.
Karthik Harve 27-Apr-13 2:59am    
check now. i provided a alias for the result set.
please check the data type of column approved_date.
It should be datetime type and not varchar.
 
Share this answer
 
Comments
Sweetynewb 27-Apr-13 2:53am    
it is datetime.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900