Sql server SQL Server分页复杂查询
我想给他的核心表格编页码。我有两张桌子: gs\U分数表Sql server SQL Server分页复杂查询,sql-server,pagination,Sql Server,Pagination,我想给他的核心表格编页码。我有两张桌子: gs\U分数表 id (auto increment int) project_id (int) game_id (int) user_id (int) entry_date (datetime) score (int) 用户 id (auto increment int) user_name (varchar) 我想要的是通过scores DESC获得他的核心列表和订单列表,但我总是在第5行中得到错误(这是:ROW\u NUMBER()(order
id (auto increment int)
project_id (int)
game_id (int)
user_id (int)
entry_date (datetime)
score (int)
用户
id (auto increment int)
user_name (varchar)
我想要的是通过scores DESC
获得他的核心列表和订单列表,但我总是在第5行中得到错误(这是:ROW\u NUMBER()(order by total\u scores DESC)作为RowNumber
),说:
列名称“total_score”无效
谁能帮忙吗
SELECT TOP 50
*
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY total_score DESC) AS RowNumber,
gs.user_id,
users.user_name,
SUM(gs.score) AS total_score,
(SELECT COUNT(gs2.id) FROM gs_score_table AS gs2 WHERE gs2.user_id = gs.user_id AND gs2.game_id = 1) AS games_played,
TotalRows=Count(*) OVER()
FROM
gs_score_table AS gs
INNER JOIN
users ON users.id = gs.user_id
WHERE
gs.project_id = 2
AND gs.game_id = 1
AND CAST(gs.entry_date AS date) BETWEEN '2012-04-23' AND '2012-04-23'
GROUP BY
gs.user_id, users.user_name) _tmpInlineView WHERE RowNumber >= 1
您不能在ROW\ U NUMBER ORDER BY子句中使用别名“total\ U score”。相反,您需要:
ROW_NUMBER() OVER (ORDER BY SUM(gs.score) DESC) AS RowNumber
非常感谢你。就这样。