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)
 Select TOP

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-07-28 : 15:19:23
Ya writes "How do you select the "LAST" or "BOTTOM" X (where X is some number of Records ie. 10) when there is NO ORDER / INDEX to order by? If there was a auto keyid index can you use that?

Also, the "WITH TIES" keyword does not seem too clear...

Thanks,
Ya"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-07-28 : 15:45:55
You can't. Since there is no order to the table, the concept of first and last is completely meaningless. In any case, a relational database (like SQL Server) does not maintain any internal identification of rows based on physical position, so again, without a specified ORDER BY clause, first and last have no meaning. If you want a particular row, you have to query for its data value(s), not its position.

You can use an autonumber/identity column as a fake row number. Take a look here:

http://www.sqlteam.com/item.asp?ItemID=1491

WITH TIES is detailed in Books Online. It is dependant upon the values of the rows being returned in the query. If you use TOP and want to return 10 rows, but there are 11 rows with the same value that would qualify for your query, WITH TIES will include the 11th row. Again, changing the ORDER BY clause will affect which rows are considered the "top" rows.

If you need 10 rows only regardless of ties, you can also use SET ROWCOUNT.

Go to Top of Page
   

- Advertisement -