Is any way that I could select specified number of rows in SQL Server? Like on my first query, I wanted to get rows 1-5, then next is rows 6-10, then onwards? Thank you in advance for your answers :)
To select duplicate values, you need to create groups of rows with the same values and then select the groups with counts greater than one. You can achieve that by using GROUP BY and a HAVING clause.
For SQL Server, a generic way to go by row number is as such: SET ROWCOUNT @row --@row = the row number you wish to work on.
For SQL Server 2012, try this (simply set the offset)
SELECT *
FROM MyTable
ORDER BY OrderingColumn ASC
OFFSET 0 ROWS
FETCH NEXT 5 ROWS ONLY
OFFSET
:
Specifies the number of rows to skip before it starts to return rows from the query expression.
FETCH NEXT
:
Specifies the number of rows to return after the OFFSET
clause has been processed.
Definitions of OFFSET
and FETCH NEXT
are from here.
Query 1:
Offset 0 => 1-5
Query 2:
Offset 5 => 6-10, etc.
SQL fiddle example: http://sqlfiddle.com/#!6/b4b8c/2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With