如何根据条件

时间:2018-01-11 21:25:59

标签: sql sql-server tsql

我有一个roleHistory表,用于跟踪所有员工的活动和非活动角色。这是我的表:

Id RoleId UserId  DateAssigned    Active 
-----------------------------------------
 1    6    824     2017-11-08       1   
 2    7    824     2017-11-08       0     
 3    6    833     2017-11-07       1    
 4    7    833     2017-11-07       0
 5    6    888     2017-11-07       0
 6    7    888     2017-11-10       0
 7    6    888     2017-11-11       1
 8    6    890     2017-11-24       0

我想通过userId和roleId对其进行分组,例如,如果userId 824在一个角色中处于活动状态而在另一个角色中处于非活动状态,我只想获得活动角色。如果用户在两个角色中都处于非活动状态,我希望不活动。

这是我到目前为止所尝试的:

 SELECT DISTINCT 
     CONVERT(varchar, p_Empl.ID) AS Id, 
     (p_empl.Last_Name + p_Empl.First_Name) AS Name, 
     RoleHistory.Active, RoleHistory.RoleId
 FROM  
     p_Empl
 INNER JOIN 
     UserProfile ON UserProfile.EmployeeID = p_Empl.ID
 INNER JOIN 
     RoleHistory ON RoleHistory.UserId = UserProfile.Id
 INNER JOIN 
     webpages_Roles ON Webpages_Roles.RoleID = RoleHistory.RoleID
 WHERE 
     (webpages_Roles.RoleName = 'Project Manager' OR 
      webpages_Roles.RoleName = 'Sales Manager') 
     AND (RoleHistory.Id IN (SELECT MAX(RoleHistory.Id) AS DateUpdated 
                             FROM RoleHistory 
                             GROUP BY UserId,RoleId))  
ORDER BY 
    Name   

这是此查询返回的结果:

 1    6    824     2017-11-08       1   
 2    7    824     2017-11-08       0     
 3    6    833     2017-11-07       1    
 4    7    833     2017-11-07       0
 6    7    888     2017-11-10       0
 7    6    888     2017-11-11       1
 8    6    890     2017-11-24       0

但我想要完成的是:

 1    6    824     2017-11-08       1     
 3    6    833     2017-11-07       1    
 7    6    888     2017-11-11       1
 8    6    890     2017-11-24       0

任何人都可以帮我实现吗?

1 个答案:

答案 0 :(得分:0)

您可以使用ROW_NUMBER()来确定活动和非活动,例如

示例数据:

        DECLARE @T TABLE
            (
              ID INT ,
              RoleID INT ,
              UserID INT ,
              DateAssigned DATETIME ,
              Active INT
            )


        INSERT  INTO @T
                ( ID, RoleID, UserID, DateAssigned, Active )
        VALUES  ( 1, 6, 824, '2017-11-08', 1 )  
        ,       ( 2, 7, 824, '2017-11-08', 0 )    
        ,       ( 3, 6, 833, '2017-11-07', 1 )   
        ,       ( 4, 7, 833, '2017-11-07', 0 )
        ,       ( 5, 6, 888, '2017-11-07', 0 )
        ,       ( 6, 7, 888, '2017-11-10', 0 )
        ,       ( 7, 6, 888, '2017-11-11', 1 )
        ,       ( 8, 6, 890, '2017-11-24', 0 )

查询:

        SELECT  ID ,
                RoleID ,
                UserID ,
                DateAssigned ,
                Active
        FROM    ( SELECT    ID ,
                            RoleID ,
                            UserID ,
                            DateAssigned ,
                            Active ,
                            ROW_NUMBER() OVER ( PARTITION BY UserID ORDER BY Active DESC ) ctr
                  FROM      @T
                ) t
        WHERE   t.ctr = 1

结果

    ID          RoleID      UserID      DateAssigned            Active
    ----------- ----------- ----------- ----------------------- -----------
    1           6           824         2017-11-08 00:00:00.000 1
    3           6           833         2017-11-07 00:00:00.000 1
    7           6           888         2017-11-11 00:00:00.000 1
    8           6           890         2017-11-24 00:00:00.000 0

    (4 row(s) affected) 

在你的情况下,这样的事情。 (假设您当前的结果基于您的选择查询)

         SELECT Id ,
                Name ,
                ACTIVE
         FROM   ( SELECT    Id ,
                            Name ,
                            ACTIVE ,
                            ROW_NUMBER() OVER ( PARTITION BY UserID ORDER BY Active DESC ) ctr
                  FROM      ( SELECT DISTINCT
                                        CONVERT(VARCHAR, p_Empl.ID) AS Id ,
                                        ( p_empl.Last_Name
                                          + p_Empl.First_Name ) AS Name ,
                                        RoleHistory.Active ,
                                        RoleHistory.RoleId,
                                        UserId 
                              FROM      p_Empl
                                        INNER JOIN UserProfile ON UserProfile.EmployeeID = p_Empl.ID
                                        INNER JOIN RoleHistory ON RoleHistory.UserId = UserProfile.Id
                                        INNER JOIN webpages_Roles ON Webpages_Roles.RoleID = RoleHistory.RoleID
                              WHERE     ( webpages_Roles.RoleName = 'Project Manager'
                                          OR webpages_Roles.RoleName = 'Sales Manager'
                                        )
                                        AND ( RoleHistory.Id IN (
                                              SELECT    MAX(RoleHistory.Id) AS DateUpdated
                                              FROM      RoleHistory
                                              GROUP BY  UserId ,
                                                        RoleId ) )
                            ) T
                ) TT
         WHERE  tt.ctr = 1
         ORDER BY TT.Name