| Author |
Topic |
|
laailalalaa
Yak Posting Veteran
57 Posts |
Posted - 2010-04-26 : 17:37:52
|
| if i have table T(id,number) how can i retrieve records from position 3 to 6 let's say, ordered by number?thank u |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-26 : 18:10:59
|
| Hi Tara,What is the difference between "BETWEEN" and ">= AND <=" in performance? |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-26 : 18:12:18
|
| [code]SELECT ID, NumberFROM ( SELECT TOP 6 ID, Number, ROW_NUMBER() OVER (ORDER BY NUMBER) AS Row FROM T ) zWHERE Row >= 3[/code]------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-26 : 18:14:18
|
quote: Originally posted by tkizer This is seriously too easy. Don't your class materials cover this?SELECT id, numberFROM TWHERE id >= 3 AND id <= 6ORDER BY number
Who said that ID contains 1, 2, 3, 4, 5, 6...?------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-26 : 18:15:56
|
quote: Originally posted by ms65g Hi Tara,What is the difference between "BETWEEN" and ">= AND <=" in performance?
I'd be surprised if there were any difference. Under the hood, I'd expect SQL Server to implement the 2 in the same way.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
laailalalaa
Yak Posting Veteran
57 Posts |
Posted - 2010-04-27 : 04:20:40
|
that's it, i didn't know about row_number() and was using rank.thanks :)quote: Originally posted by DBA in the making
SELECT ID, NumberFROM ( SELECT TOP 6 ID, Number, ROW_NUMBER() OVER (ORDER BY NUMBER) AS Row FROM T ) zWHERE Row >= 3 ------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee.
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 04:25:12
|
quote: Originally posted by laailalalaa that's it, i didn't know about row_number() and was using rank.thanks :)quote: Originally posted by DBA in the making
SELECT ID, NumberFROM ( SELECT TOP 6 ID, Number, ROW_NUMBER() OVER (ORDER BY NUMBER) AS Row FROM T ) zWHERE Row >= 3 ------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee.
If number contains unique values then rank() will also work------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
laailalalaa
Yak Posting Veteran
57 Posts |
Posted - 2010-04-27 : 04:50:07
|
i know it would work, but number doesn't contain unique values (i simplified the table schema, the table i'm working with is more complicated).thanks for your helpquote: Originally posted by visakh16
quote: Originally posted by laailalalaa that's it, i didn't know about row_number() and was using rank.thanks :)quote: Originally posted by DBA in the making
SELECT ID, NumberFROM ( SELECT TOP 6 ID, Number, ROW_NUMBER() OVER (ORDER BY NUMBER) AS Row FROM T ) zWHERE Row >= 3 ------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee.
If number contains unique values then rank() will also work------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
|