Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Select top 10-20 results?

Tags:

sql

sql-server

I have two columns the first one I want top 10 products (1-10)

That is

SELECT TOP 10 * FROM Product    

In the second column I want the next 10 results (11-20)

How do I do that?

like image 659
Nicklas Avatar asked Sep 07 '11 10:09

Nicklas


People also ask

What does select top 10 do in SQL?

It will return the top number of rows in the result set based on top_value. For example, TOP(10) would return the top 10 rows from the full result set. Optional.

How do I get next 100 records in SQL?

select <column list you want> from <your table name> order by ProductName offset 100 rows fetch next 100 rows only; That will skip the first 100 rows (in order by ProductName) and return the next 100 rows.


1 Answers

WITH T AS ( SELECT TOP 20 name,         row_number() OVER (ORDER BY id) AS RN FROM Products ORDER BY id ) SELECT         MAX(CASE WHEN RN <=10 THEN name END) AS Col1,        MAX(CASE WHEN RN > 10 THEN name END) AS Col2 FROM T        GROUP BY RN % 10 
like image 93
Martin Smith Avatar answered Oct 14 '22 16:10

Martin Smith