SQL Server:排除不同的列行号?

时间:2018-12-06 09:35:53

标签: sql-server

如何排除不同的列行号?

select语句如下:

SELECT *
FROM
    (SELECT DISTINCT TOP 100   
         ROW_NUMBER() OVER (ORDER BY Cases.CreatedDate DESC) as row,
         Cases.status, Cases.CreatedDate, Cases.DWFCaseId, 
         Resource.ResourceInfo AS ResourceFullName,
         actions.ActionDate, Action.ActionDuedate 
     FROM
         Cases 
     INNER JOIN
         ResourceInfo ON Cases.caseid = ResourceInfo.caseid 
     LEFT OUTER JOIN
         actions ON actions.ActionId = ResourceInfo.ActionId
     WHERE     
         (actions.ActionType = 2)
    )

2 个答案:

答案 0 :(得分:0)

从子查询中取出它:

select ROW_NUMBER() OVER (ORDER BY   x.CreatedDate desc ) as row, x.*
from (
    SELECT  DISTINCT top (100)
        Cases.status,
        Cases.CreatedDate,
        Cases.DWFCaseId,
        Resource.ResourceInfo AS ResourceFullName,
        actions.ActionDate,
        Action.ActionDuedate
    from Cases
            inner join ResourceInfo  on Cases .caseid = ResourceInfo .caseid
            left outer join actions on actions.ActionId = ResourceInfo .ActionId
    WHERE     (actions.ActionType = 2)
) x

答案 1 :(得分:0)

在外部查询中而不是子查询中使用window函数

SELECT *,
       ROW_NUMBER() OVER (ORDER BY CreatedDate DESC) as row,
FROM
    (
     SELECT DISTINCT TOP 100   
            Cases.status, 
            Cases.CreatedDate, 
            Cases.DWFCaseId, 
            Resource.ResourceInfo AS ResourceFullName,
            actions.ActionDate, 
            Action.ActionDuedate 
     FROM
         Cases 
     INNER JOIN
         ResourceInfo ON Cases.caseid = ResourceInfo.caseid 
     LEFT OUTER JOIN
         actions ON actions.ActionId = ResourceInfo.ActionId
     WHERE actions.ActionType = 2
    ) T; --Don't forget to use an alias for the subquery