0

I've tried to do a pagination on data in using ROW_NUMBER()
Here is my query:

SELECT * FROM
    (SELECT ROW_NUMBER() OVER (ORDER BY OrderID) AS Row,* FROM SpecificOrders) 
    AS EMP
        inner join Users as c on EMP.UserID = c.UserID
        inner join Users as u on EMP.CreatedBy = u.UserID       
        inner join SpecificOrderPayment as p on EMP.OrderID= p.OrderID

    WHERE Row BETWEEN 0 AND 10 

When I execute this query, I get output like following with :

Row | OrderID | UserID |
1   |         |        |
5   |         |        |
6   |         |        |
7   |         |        |
8   |         |        |
9   |         |        |
10  |         |        |

If I remove this WHERE Row BETWEEN 0 AND 10 condition then it'll gives me all records

Here my question is why I get only 7 rows and why here 2,3 and 4 is missing in the row column.

Moreover, If i remove 3rd join query (SpecificOrderPayment)then it will give me proper result.

11
  • 4
    There seems to be no matching records in SpecificOrderPayment for the cases that you mention. Try with LEFT instead of INNER join. Sep 14, 2016 at 10:09
  • How to use row number function?? visit this link stackoverflow.com/questions/961007/how-do-i-use-row-number Sep 14, 2016 at 10:10
  • @GiorgosBetsos, Yes, got it. Sep 14, 2016 at 10:10
  • @MukeshKalgude How to use row number function? No, the question is different. Sep 14, 2016 at 10:12
  • 1
    Also I have to point here that if you are using SQL Server 2012 and up, you could use OFFSET/FETCH to implement the pagination.
    – S.Karras
    Sep 14, 2016 at 10:25

2 Answers 2

2

you've got OrderID that are null or blanks in SpecificOrders and they are sorting to the top - the approach isn't wrong otherwise, although there are other ways of doing it such as TOP 10..etc

SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY OrderID) AS Row,* FROM SpecificOrders 
                                                  WHERE RTRIM(COALESCE(OrderID, '')) <> '') 
AS EMP
    inner join Users as c on EMP.UserID = c.UserID
    inner join Users as u on EMP.CreatedBy = u.UserID       
    inner join SpecificOrderPayment as p on EMP.OrderID= p.OrderID

WHERE Row BETWEEN 0 AND 10 
6
  • It's worked, but what if i have one an other table called SpecificOrderDetails and which contains multiple record with single orderID, how can use this? Sep 14, 2016 at 10:52
  • 1
    @div you could join using OUTER APPLY and join to only the TOP 1 - I'm not sure of what you want to achieve though, what info do you want from SpecificOrderDetails ?
    – Cato
    Sep 14, 2016 at 10:55
  • I don't know the structure of tables, but I thought that OrderId was something like a not nullable (primary?) key in table SpecificOrders, in this case, the problem is not there but in other joined table, the most probably is SpecificOrderPayment because (I think) payment could be not mandatory for the order, while user should be. the last inner join could be dangerous..
    – MtwStark
    Sep 30, 2016 at 8:23
  • @div can you pleas run this query? select count(*) from SpecificOrders where OrderID is null and post result?
    – MtwStark
    Sep 30, 2016 at 8:29
  • @div no problem, I was just wondering about the solution of Andrew. He thinks the problem is that SpecificOrders.OrderID can be NULL, so he suggests to filter it before to INNER join other tables. I think that the problem is the missing of a related SpecificOrderPayment record or (less probably) nulls in SpecificOrders.UserID and SpecificOrders.CreatedBy columns, but In every case the inner joins will filter out from result all orders without a payment or with null userid and createdby so, be sure to check for it.
    – MtwStark
    Sep 30, 2016 at 8:49
2

The problem is that you are numbering the rows of SpecificOrders and not of final result.

In your case you only have one row per order, so using left joins should solve the issue

But, if the inner query could return multiple rows for each OrderID you will see the same row number many times

And if the join (inner) will filter some row you will not get that row number in result.

You have simply to separate the query for data extraction from the query for pagination,

Try this:

SELECT * 
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY EMP.OrderID) AS Row, EMP.*
    FROM SpecificOrders AS EMP
    left join Users as c on EMP.UserID = c.UserID
    left join Users as u on EMP.CreatedBy = u.UserID       
    left join SpecificOrderPayment as p on EMP.OrderID= p.OrderID
) D
WHERE [Row] BETWEEN 0 AND 10 
2
  • the error is because with SELECT * FROM you get same column name many times, I will correct the SELECT to avoid the issue
    – MtwStark
    Sep 14, 2016 at 12:39
  • glad to be of any help. after EMP.* add the columns you need from the tables, and then, if you like this solution, please accept it as best answer
    – MtwStark
    Sep 14, 2016 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.