sql - Limit the number of rows per ID -
i trying limit number of rows per case 5 rows. cases have 1 or 2 rows have 15 or more.
this example of stored procedure using count number of rows per case.
select row_number() over(partition rce.reportruncaseid order rce.reportruncaseid) row, rce.reportruncaseid caseid, year(rce.ecodate) ecoyear phdrpt.reportcaselist rcl inner join phdrpt.rptcaseeco rce on rce.reportid = rcl.reportid , rce.reportruncaseid = rcl.reportruncaseid group rce.reportid, rce.reportruncaseid, year(rce.ecodate) order rce.reportruncaseid, year(rce.ecodate)
here screenshot of stored procedure produces: screenshot
i have tried use clause not allow me place window function after clause. not recognize "row" alias.
is there way count number or rows per case (instead of window function) can use clause? or there way limit records per case 5 using existing stored procedure?
thank in advance help.
you can wrap statement in cte
since sql server
supports it.
with records ( select row_number() over(partition rce.reportruncaseid order rce.reportruncaseid) row, rce.reportruncaseid caseid, year(rce.ecodate) ecoyear phdrpt.reportcaselist rcl inner join phdrpt.rptcaseeco rce on rce.reportid = rcl.reportid , rce.reportruncaseid = rcl.reportruncaseid group rce.reportid, rce.reportruncaseid, year(rce.ecodate) ) select caseid, ecoyear records row <= 10 order caseid, ecoyear