我无法在同一个select语句中的行号上使用where条件。如果我使用不同的select语句在rownumber上应用条件,结果就不一致了......
quantity
SELECT TOP (@lastrow - 1) c.totalRows
,c.ae_effective_enrollment_id
,c.[user_id]
,c.login_name
,c.first_name
,c.last_name
,cm.courseware_title
,cm.courseware_code
,@courseware_id assetId
,c.enrollment_status_id
,CASE
WHEN c.enrollment_status_id = 2
AND c.is_self_enrolled = 0
THEN 'Admin-' + s.description
WHEN c.enrollment_status_id = 2
AND c.is_self_enrolled = 1
THEN 'Self-' + s.description
ELSE s.description
END AS enrollmentStatus
,c.is_group
,CASE
WHEN c.is_self_enrolled = 0
THEN 1
ELSE 0
END is_admin
,CASE
WHEN c.auma_is_assigned = 1
THEN 'Admin-assigned'
WHEN c.auma_is_assigned = 0
THEN 'Self-assigned'
ELSE 'No-My-Plan'
END AS myplanStatus
, master_assignment_id
,ROW_NUMBER() over(partition by cm.courseware_id,c.user_id order by c.is_self_enrolled)as check_row
FROM enrollmentCTE c
INNER JOIN dbo.courseware_master cm ON cm.courseware_id = @courseware_id
LEFT JOIN @statuscodes s ON s.id = c.enrollment_status_id
WHERE check_row=1 and
enrollment_status_id<>4 and
rownumber > @firstrow
AND rownumber < @lastrow
ORDER BY rownumber
此处无法识别。请帮忙
答案 0 :(得分:2)
SQL执行顺序。
FROM
条款WHERE
条款GROUP
BY条款HAVING
条款SELECT
条款- 醇>
ORDER BY
条款
check_row别名是在 select
部分中进行的,因此它在上下文中尚不存在
修改强> 做了一些测试。似乎无法做对。作为临时解决方案,您可以尝试使用
ROW_NUMBER() over(...
where
子句中的以及
修改强> MSDN website中的另一个选项是
返回行的子集
以下示例按OrderDate的顺序计算SalesOrderHeader表中所有行的行号,并仅返回50到60行。
USE AdventureWorks2012;
GO
WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
ROW_NUMBER() OVER (ORDER BY OrderDate) AS RowNumber
FROM Sales.SalesOrderHeader
)
SELECT SalesOrderID, OrderDate, RowNumber
FROM OrderedOrders
WHERE RowNumber BETWEEN 50 AND 60;
答案 1 :(得分:0)
SELECT totalRows, ae_effective_enrollment_id, user_id, login_name, first_name, last_name, check_row FROM
(SELECT TOP (@lastrow - 1) c.totalRows as totalRows
,c.ae_effective_enrollment_id as ae_effective_enrollment_id
,c.[user_id] as user_id
,c.login_name as login_name
,c.first_name as first_name
,c.last_name as last_name
,cm.courseware_title as courseware_title
,cm.courseware_code as courseware_code
,@courseware_id as assetId
,c.enrollment_status_id as enrollment_status_id
,CASE
WHEN c.enrollment_status_id = 2
AND c.is_self_enrolled = 0
THEN 'Admin-' + s.description
WHEN c.enrollment_status_id = 2
AND c.is_self_enrolled = 1
THEN 'Self-' + s.description
ELSE s.description
END AS enrollmentStatus
,c.is_group
,CASE
WHEN c.is_self_enrolled = 0
THEN 1
ELSE 0
END is_admin
,CASE
WHEN c.auma_is_assigned = 1
THEN 'Admin-assigned'
WHEN c.auma_is_assigned = 0
THEN 'Self-assigned'
ELSE 'No-My-Plan'
END AS myplanStatus
, master_assignment_id
,ROW_NUMBER() over(partition by cm.courseware_id,c.user_id order by c.is_self_enrolled)as check_row
FROM enrollmentCTE c
INNER JOIN dbo.courseware_master cm ON cm.courseware_id = @courseware_id
LEFT JOIN @statuscodes s ON s.id = c.enrollment_status_id
WHERE enrollment_status_id<>4 and
rownumber > @firstrow
AND rownumber < @lastrow
ORDER BY rownumber ) t where check_row = 1
注意 - 在第一个选择语句中添加所有列名
答案 2 :(得分:0)
使用CTE
根据其他
;WITH CTE AS(
SELECT c.totalRows
,c.ae_effective_enrollment_id
,c.[user_id]
,c.login_name
,c.first_name
,c.last_name
,cm.courseware_title
,cm.courseware_code
,@courseware_id assetId
,c.enrollment_status_id
,CASE
WHEN c.enrollment_status_id = 2
AND c.is_self_enrolled = 0
THEN 'Admin-' + s.description
WHEN c.enrollment_status_id = 2
AND c.is_self_enrolled = 1
THEN 'Self-' + s.description
ELSE s.description
END AS enrollmentStatus
,c.is_group
,CASE
WHEN c.is_self_enrolled = 0
THEN 1
ELSE 0
END is_admin
,CASE
WHEN c.auma_is_assigned = 1
THEN 'Admin-assigned'
WHEN c.auma_is_assigned = 0
THEN 'Self-assigned'
ELSE 'No-My-Plan'
END AS myplanStatus
, master_assignment_id
,ROW_NUMBER() over(partition by cm.courseware_id,c.user_id order by c.is_self_enrolled) as check_row
FROM enrollmentCTE c
INNER JOIN dbo.courseware_master cm ON cm.courseware_id = @courseware_id
LEFT JOIN @statuscodes s ON s.id = c.enrollment_status_id
WHERE enrollment_status_id<>4
AND rownumber > @firstrow
AND rownumber < @lastrow
)
SELECT TOP (@lastrow - 1) *
FROM CTE
WHERE check_row = 1
ORDER BY rownumber