Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
;with One as
(
    select top 100 percent 1 as t,t1.regId as a, t1.visitCount as b
     from registerdata as t1 order by t1.visitCount desc 
)
,Two as
(
    select top 100 percent 2 as t,t1.regId, t1.visitCount
    from registerdata as t1
)
select * from One
union
select * from Two order by t desc


this is my query and i want first query order by . it's run but result are shown wrong ..
please give any solution.
Posted
Updated 10-Jul-14 20:57pm
v3
Comments
Dinesh.V.Kumar 11-Jul-14 2:05am    
Order BY clause should come outside as shown below

select * from (select top 100 percent 1 as t,t1.regId, t1.visitCount
from registerdata as t1
) as yy order by t1.visitCount desc
Try this and let me know if its working

Regards,
Dinesh Kumar.V.
Yogesh_Chavan 11-Jul-14 2:13am    
The multi-part identifier "t1.visitCount" could not be bound

its show me error

Hi,

Use below Query

SQL
select * from (select top 100 percent 1 as t,t1.regId, t1.visitCount
    from registerdata) as t1
   order by t1.visitCount desc
 
Share this answer
 
Hi,

I am not getting, why are you using sub-query? Your simple query will work and give the required output.

SQL
select top 100 percent 1 as t,t1.regId, t1.visitCount from registerdata as t1 order by t1.visitCount desc




Also check below query if you are using union...

now try this....just added desc with 1 in order by clause.

SQL
select top 100 percent 1 as t,t1.regId, t1.visitCount from registerdata as t1 
union
--here i am assuming next part of your query.
select top 100 percent 2 as t,t2.regId, t2.visitCount from table2 as t2 
order by  1 desc , 3 desc





Hope this will help you.
 
Share this answer
 
v3
Comments
Yogesh_Chavan 11-Jul-14 2:27am    
i am also use a union and i want first query order by
Magic Wonder 11-Jul-14 2:34am    
Kindly mention what are you trying exactly? If possible edit your question and provide your query.
Yogesh_Chavan 11-Jul-14 2:53am    
it's not get me proer result...
i want show second query result on first after first query result shown in order by..
Magic Wonder 11-Jul-14 2:59am    
Do you mean to say data of 2nd part of union should be displayed first and then data of first part of union?

am i correct?
Yogesh_Chavan 11-Jul-14 3:03am    
yes right ...and first part of union data should be order by
Try this code....

SELECT * FROM (
select top 100 percent 1 as t,t1.regId, t1.visitCount
from registerdata as t1
) as yy ORDER BY yy.visitCount desc
 
Share this answer
 
SQL
;with One as
(
    select top 100 percent 1 as t,t1.regId as a, t1.visitCount as b
     from registerdata as t1 order by t1.visitCount desc
)
,Two as
(
    select top 100 percent 2 as t,t1.regId, t1.visitCount
    from registerdata as t1
)
select * from One
union all
select * from Two order by t desc
 
Share this answer
 
Comments
Magic Wonder 14-Jul-14 1:35am    
Did you solve your problem?

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