February 27, 2013 at 2:40 pm
One table as below.
How to code to select top 2 records?
IDSEQUECENOTE
10011AAA
10012BBB
10013CCC
10014DDD
10015EEE
20021GGG
20022HHH
20023KKK
20024MMM
20025NNN
20026PPP
20027QQQ
20028RRR
I need to select top 2 records:
IDSEQUECENOTE
10014DDD
10015EEE
20027QQQ
20028RRR
February 27, 2013 at 2:52 pm
adonetok (2/27/2013)
One table as below.How to code to select top 2 records?
IDSEQUECENOTE
10011AAA
10012BBB
10013CCC
10014DDD
10015EEE
20021GGG
20022HHH
20023KKK
20024MMM
20025NNN
20026PPP
20027QQQ
20028RRR
I need to select top 2 records:
IDSEQUECENOTE
10014DDD
10015EEE
20027QQQ
20028RRR
Something like this should work.
select * from
(
select ID, SEQUECE, NOTE, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY SEQUECE) as RowNum
From YourTable
) x
where x.RowNum <= 2
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
February 27, 2013 at 3:04 pm
I ran it but it only lists the first 2 records.
How to select the last 2 (the highest 2 sequence)?
February 27, 2013 at 3:07 pm
I got it.
Just add "ORDER BY SEQUENCE desc" it works great.
Thanks
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply