问题描述
|
我使用ROW_NUM()函数在ASP.NET中进行分页
SELECT row_num,expense_id,email,reason,amount,date,category_name,is_income
FROM
(
SELECT e.expense_id,e.email,e.reason,e.amount,e.date,c.category_name,e.is_income,ROW_NUMBER() OVER(ORDER BY e.date DESC,e.expense_id) as row_num
FROM Expense e
JOIN Category c ON e.category_id = c.category_id
WHERE e.date >=\'5-1-2011\' AND e.date<=\'5-31-2011\'
) as ExpenseInfo
WHERE email=\'[email protected]\'
但是它返回了一个不一致的row_num列表(例如1,3,4 ...或2,4 ...)。我怎么解决这个问题?
提前致谢。
我解决了我的问题
SELECT RowNum,is_income
FROM
(
SELECT e.expense_id,Row_Number() OVER(ORDER BY date DESC) as row_num
FROM Expense e JOIN Category c
ON e.category_id = c.category_id
WHERE e.date >=\'5-1-2011\' AND e.date<=\'5-31-2011\' AND e.email=\'[email protected]\'
) AS ExpenseInfo
WHERE row_num>=1 and row_num<=20
解决方法
Where email = \'[email protected]\'
正在过滤掉某些行。对于您在这里所做的事情,不需要“外部”查询。只需将它们包装在一起。
SELECT ROW_NUMBER() OVER(ORDER BY e.date DESC,e.expense_id) as row_num,e.expense_id,e.email,e.reason,e.amount,e.date,c.category_name,e.is_income
FROM Expense e
JOIN Category c
ON e.category_id = c.category_id
WHERE e.date >=\'5-1-2011\'
AND e.date <=\'5-31-2011\'
AND e.email = \'[email protected]\'
, 在主查询中添加ROW_NUMBER。当您将其添加到子查询中时,首先生成数字,然后过滤记录。当您在最终结果上生成行号时,就不会有此问题。
select
s.*,row_number() over(order by s.date desc,s.expense_id) as row_num
from
(
/*YourQueryWithAllItsConditions*/
) s