Reputation: 1751
I have this query below and I need to get specific amounts of rows from it also, like 100 rows. I also need to get the rows from a specific row count like from 100 to 200, from 200 - 300 etc. I have been experimenting with ROW_NUMBER()
function without luck.
NB! Cannot us "offset 100 rows fetch next 100 rows only" because it only works in mssql 2012.
SELECT
Result.CaseCount,
CaseID,
ChildCaseName,
CPR,
FirstName,
LastName,
CustomerInfoID,
InstitutionName,
CaseDeadline,
[StatusName],
[StatusOwner]
FROM ResultSelectorCTE,
(select count(*) as CaseCount FROM ResultSelectorCTE) AS Result
How can I do this?
Upvotes: 2
Views: 3691
Reputation: 1062502
I also need to get the rows from a specific row count like from 100 to 200, from 200 - 300 etc.
select Foo, Bar
from SomeTable
order by Blap
offset 100 rows fetch next 100 rows only
Or to do it with ROW_NUMBER()
:
select x.Foo, x.Bar
from (
select Foo, Bar, ROW_NUMBER() over(order by Blap desc) as [Row]
from SomeTable) x
where x.[Row] > 100 and x.[Row] <= 200
order by x.[Row]
Upvotes: 5
Reputation: 7766
Select TOP 100 [column1],[column2].....FROM TABLE
but it will depends on the query you are giving.
If no orderby
is there it will pull different result each time..
Upvotes: 0
Reputation: 77285
SELECT TOP X Result.CaseCount, CaseID, ChildCaseName, ...
Where X is your number.
Upvotes: 1
Reputation: 45947
MSSQL: SELECT TOP 100 Result.CaseCount,...
MYSQL: SELECT ... LIMIT 100;
Upvotes: 3