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
 SQL Server Development (2000)
 return top rows in a single query without using TOP N or set rowcount = N

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-13 : 08:59:36
Mike writes "I have to return top rows in a single query without using TOP N or SET rowcount = N. Two cases: 1. there are unique field(s)in the table; 2. there are duplicate rows in the table."

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2002-03-13 : 08:59:36
This reads much like a home work question. What are you trying to do?
Go to Top of Page

dsdeming

479 Posts

Posted - 2002-03-13 : 13:39:51
Without TOP N or SET ROWCOUNT, your're left with ugly choices. Here are 2:

You could build a temp table with an identity column and columns corresponding to the top values ( say SerialNo ) you need to return. Insert the SerialNo values from your source table into the temp table. Join the temp table to your source table on SerialNo = SerialNo and return all rows from the source table using a where clause like 'WHERE #tmp.ID <= 10'.

Alternatively, you could bail out of a cursor after x iterations....
No, forget I even mentioned it. The cursor police will be knocking on my door.

Go to Top of Page

levinmik
Starting Member

1 Post

Posted - 2002-03-13 : 22:39:31
quote:

This reads much like a home work question. What are you trying to do?

It's a prospective interview question. There is a mainframe database that has no TOP and Rowcount support. So, they use a single query to do that.



Go to Top of Page
   

- Advertisement -