Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Selecting Top 2 record

Author  Topic 

sagavb
Starting Member

18 Posts

Posted - 2009-06-01 : 00:31:54
Hi there,

Is it possible to get top 2 record from below table

Table name: test

name sal
Mr.X 800
Mr.Y 1000
Mr.Z 1500
Mr.A 2000

Expected output should be as

name sal
Mr.Y 1000

Thanks in advance
Saga

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-06-01 : 01:07:18
Saga sir,

do u have any identity column or primary key?


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-01 : 01:15:26
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspx


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-06-01 : 01:23:22
Try this!


select top 1 from
(select top 2 * from test order by sal) t order by sal desc


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled
Go to Top of Page

sagavb
Starting Member

18 Posts

Posted - 2009-06-01 : 01:36:48
Senthil Sir,

I dont think it will work.

Can you copy that query and run it?

Regards
Saga

quote:
Originally posted by senthil_nagore

Try this!


select top 1 from
(select top 2 * from test order by sal) t order by sal desc


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-01 : 01:37:55
quote:
Originally posted by senthil_nagore

Try this!


select top 1 * from
(select top 2 * from test order by sal) t order by sal desc


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-06-01 : 01:40:22
saga sir,

just i miss the "*" as khtan pointed out... u must debug it saga...
else ask ur boss...


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled
Go to Top of Page

sagavb
Starting Member

18 Posts

Posted - 2009-06-01 : 01:53:13
Hey Thanks la


quote:
Originally posted by senthil_nagore

saga sir,

just i miss the "*" as khtan pointed out... u must debug it saga...
else ask ur boss...


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-06-01 : 02:38:00
quote:
Originally posted by sagavb

Hi there,

Is it possible to get top 2 record from below table

Table name: test

name sal
Mr.X 800
Mr.Y 1000
Mr.Z 1500
Mr.A 2000

Expected output should be as

name sal
Mr.Y 1000

Thanks in advance
Saga


Your expected output contradicts your topic "Selecting Top 2 record"
What basis do you want to get it?



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sagavb
Starting Member

18 Posts

Posted - 2009-06-01 : 02:45:21
Hi Madhivanan,

I meant, selecting 2nd record from a table

Regards
Saga

quote:
Originally posted by madhivanan

quote:
Originally posted by sagavb

Hi there,

Is it possible to get top 2 record from below table

Table name: test

name sal
Mr.X 800
Mr.Y 1000
Mr.Z 1500
Mr.A 2000

Expected output should be as

name sal
Mr.Y 1000

Thanks in advance
Saga


Your expected output contradicts your topic "Selecting Top 2 record"
What basis do you want to get it?



Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-06-01 : 04:30:18
What basis do you need second record?


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -