Buddies,I have a sample scripts like:create table #source( [cuslink] int, [sequence] int)insert into #source values (2233, 1)insert into #source values (2233, 2)insert into #source values (2233, 3)declare @cuslink intset @cuslink=2233
And I expect the output should be (order of 'Sequence' in output is important):cuslink sequence ----------- ----------- 2233 22233 1
I run the 3 following scripts. 2 of them give me wrong output; the the last script gives me right answer. I do not know how they really work. Thanks for your ideas.-- Wrong output. Why?select top 2 * from #sourcewhere cuslink=@cuslinkorder by sequence desc-- Wrong output. Why?select top 2 *from( select top 2 * from #source where cuslink=@cuslink) worder by sequence desc-- Right output. Why?select top 2 *from( select top 2 * from #source where cuslink=@cuslink order by sequence) worder by sequence desc-- dropdrop table #source