| Author |
Topic |
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2009-08-22 : 05:19:53
|
Hi all,now, I am NOT writing a wrong simple article about "retrieve a specific row from a result set".And I do not want you post any idea or link to other similar article about it.Consider following table and result. I want to retrieve only row number 2 ('Peter Larsson') in result set.A simple method is using CURSOR with FETCH ABSOLUTE N.And when the SELECT STATEMENT has an ORDER cause then another method is using ROW_NUMBER with OVER cause.And a bad solution is using IDENTITIY INTO () function, it is bad because the inserting is an expensive job for system.And when the Query has a ORDER then a very simple method is using two TOP.------------------------------------------DECLARE @MyTable TABLE( [Value] NVarChar(25) Primary KEY )---------------------------------------------INSERT INTO @MyTable VALUES ('Peter Larsson');INSERT INTO @MyTable VALUES ('Joe Celko');INSERT INTO @MyTable VALUES ('Dr.Codd');INSERT INTO @MyTable VALUES ('C.J Date');----------------------------------------SELECT * FROM @MyTableORDER BY SubString([Value],2,1)/*Value-------------------------C.J. DatePeter LarssonJoe CelkoDr.Codd*/-----------------------------------------DECLARE MyCursor CURSOR KEYSET FOR SELECT * FROM @MyTable ORDER BY SubString([Value],2,1)OPEN MyCursorFETCH ABSOLUTE 2 FROM MyCursorCLOSE MyCursorDEALLOCATE Mycursor-----------------------------------------SELECT *FROM( SELECT *, ROW_ID=ROW_NUMBER() OVER(ORDER BY SubString([Value],2,1)) FROM @MyTable) AS DWHERE ROW_ID=2;------------------------------------------SELECT *, ROW_ID=IDENTITY(int,1,1)INTO #tempTableFROM @MyTableORDER BY SubString([Value],2,1)SELECT *FROM #tempTableWHERE ROW_ID=2DROP TABLE #tempTable--------------------------------------------SELECT TOP 1 *FROM( SELECT TOP 2 * FROM @MyTable ORDER BY SubString([Value],2,1) ASC) AS DORDER BY SubString([Value],2,1) DESC |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-22 : 10:06:25
|
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspxmadhi got varies of solution too.... Hope can help...but advise to wait pros with confirmation... |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2009-08-23 : 02:35:33
|
| WTF? If you know what row you want just select it. I can't imagine how it could be useful to select "a specific row from a result set". What you would be doing is getting the thing that owns the (MAX thing) where (thing is < MAX thing). It's not 'the second value in a record set'. That would be a strange approach. Having said that, I'd personally use the where row_number() = 2 if I truly ever needed to do this. |
 |
|
|
IncisiveOne
Starting Member
36 Posts |
Posted - 2009-08-23 : 06:13:50
|
| Exactly.On what exact basis, do you choose the "second" row of the result set ?!?!?!?What happens when the database (it changes, you know) has 20 or twenty thousand rows in the result set ... how do you "know" which one you want; do you still want Peter Larsson; or whatever row is second in the list.In relational databases we find things using keys. In record-based filing systems, we find things using "row_id" and record id ... which of course, changes all the time. Which is why people like natural keys. CheersAnything worth doing, is worth doing right. |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-23 : 22:20:01
|
Peter Larsson??i thought that was peso??? Hope can help...but advise to wait pros with confirmation... |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2009-08-24 : 02:51:25
|
Take it easy! it's just a fun |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-08-24 : 03:07:23
|
quote: Originally posted by ms65g Hi all,now, I am NOT writing a wrong simple article about "retrieve a specific row from a result set".And I do not want you post any idea or link to other similar article about it.Consider following table and result. I want to retrieve only row number 2 ('Peter Larsson') in result set.A simple method is using CURSOR with FETCH ABSOLUTE N.And when the SELECT STATEMENT has an ORDER cause then another method is using ROW_NUMBER with OVER cause.And a bad solution is using IDENTITIY INTO () function, it is bad because the inserting is an expensive job for system.And when the Query has a ORDER then a very simple method is using two TOP.
So what do you need now ? You already have the solution to retrieve row number 2. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2009-08-24 : 06:47:19
|
quote: Originally posted by ms65g Hi all,now, I am NOT writing a wrong simple article about "retrieve a specific row from a result set".
Yes you are. You are writing an article where the premise of how to address a particular problem is wrong when your tool is a relational database. |
 |
|
|
IncisiveOne
Starting Member
36 Posts |
Posted - 2009-08-24 : 12:21:41
|
quote: Yes you are. You are writing an article where the premise of how to address a particular problem is wrong when your tool is a relational database.
Or worse, you are writing an article about databases without an understanding of databases and database theory.Anything worth doing, is worth doing right. |
 |
|
|
|