I have a problem with the following (very simplified) SQL Server code. I have a webpage that displays (distinct) cases. The page has paging implemented,so each time a user clicks next or previous, sequence start and end for the next records is submitted to the store procedure.I have the below tables (again simplified). A Case can contain multiple orderlines and multiple orderlines in same case can be assigned to same user.[b]tblCase:[/b]CaseID, CreateDate150, 01/01/2016151, 02/02/2016152, 03/03/2016[b]tblCaseOrderLine[/b]CaseOrderLine, CaseId, UserID1,150,12552,150,12553,151,14324,152,12555,152,19186,152,1918I have this SQL for retrieving and filtering the cases shown:WITH cte AS ( SELECT C.CaseId, CONVERT(INT,ROW_NUMBER() OVER(ORDER BY C.createDate ASC)) AS sequenceId FROM tblCase C INNER JOIN tblCaseOrderline CO ON C.caseId = CO.CaseId WHERE (CO.UserId = @userid OR @userid IS NULL) ) SELECT * FROM cte WHERE (sequenceId BETWEEN @sequenceStart AND @sequenceEnd) Order BY sequenceIdIf I pass these parameters: @sequenceStart = 1, @sequenceEnd = 10, @userid = NULLThen I get this result:CaseId, SequenceId150, 1150, 2151, 3152, 4152, 5152, 6The wanted result is:CaseId, SequenceId150, 1151, 2152, 3Problem is that CaseID: 150 is twice in the resultset and CaseID: 152 is 3 times in the resultset because they have multiple caseorderlines - but duplicate records destroys paging as only unique caseid's is displayed on the webpage.IS there anyway I can join tblCase AND tblCaseOrderline WITHOUT making it produce duplicate records? There's a lot of examples on how to implement paging and sorting using CTE - but it's alwasy on one table only and not on tables that join with other tables.
↧