SQL Query-Group by和filters

时间:2015-03-26 21:16:11

标签: sql sql-server group-by

我有如下图所示的表格。

enter image description here

我正在尝试获取最新的status_id,按team_id分组并基于created_date的最大值。

 --Latest status for each team based on created date
SELECT *
FROM  ProductionHistory
WHERE created_Date IN (
                      SELECT MAX(created_Date)
                      FROM  ProductionHistory 
                      GROUP BY TeamID
                      )

没关系,结果如下:

enter image description here

我的问题是:

  1. 如何忽略assigned_to null然后找到团队的最新记录?
  2. 当团队中只有一条assigned_to null的记录时,请考虑这一点。
  3. 例如:对于Team_id 5,只有一个记录的assigned_to为null,我们没有team_id 5的任何其他记录,assign_to设置了一些值。 所以我们考虑过这个。

    对于团队ID 3查询结果应忽略记录4(尽管最大日期),查询结果应包含记录2.

    这是我期待的最终结果:

    enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个:

DECLARE @t TABLE
    (
      TicketID INT ,
      TeamID INT ,
      StatusID INT ,
      created_Date DATETIME ,
      Assigned_to NVARCHAR(MAX)
    )

INSERT  @t
VALUES  ( 10000, 2, 7, '2015-03-26 01:48:00.000', 'W3ER45' ),
        ( 10000, 3, 7, '2015-03-26 05:48:00.000', 'YU67IO' ),
        ( 10000, 2, 9, '2015-03-26 03:48:00.000', 'HJKO98' ),
        ( 10000, 3, 9, '2015-03-27 03:48:00.000', NULL ),
        ( 10000, 4, 11, '2015-03-23 03:48:00.000', 'GHR67' ),
        ( 10000, 5, 11, '2015-03-27 02:12:56.910', NULL );


;
WITH    cte
          AS ( SELECT   * ,
                        ROW_NUMBER() OVER ( PARTITION BY TicketID, TeamID ORDER BY CASE
                                                              WHEN Assigned_to IS NULL
                                                              THEN 1
                                                              ELSE 0
                                                              END, created_Date DESC ) AS rn
               FROM     @t
             )
    SELECT  TicketID ,
            TeamID ,
            StatusID ,
            created_Date ,
            Assigned_to
    FROM    cte
    WHERE   rn = 1

输出:

TicketID    TeamID  StatusID    created_Date            Assigned_to
10000       2       9           2015-03-26 03:48:00.000 HJKO98
10000       3       7           2015-03-26 05:48:00.000 YU67IO
10000       4       11          2015-03-23 03:48:00.000 GHR67
10000       5       11          2015-03-27 02:12:56.910 NULL

答案 1 :(得分:0)

试试这个答案。我用光标

做了
DECLARE @t TABLE ( TicketID     INT
,                  TeamID       INT
,                  StatusID     INT
,                  created_Date DATETIME
,                  Assigned_to  NVARCHAR(MAX) )
DECLARE cur CURSOR READ_ONLY FAST_FORWARD
FOR SELECT [TeamID]
FROM TT
DECLARE @i int
OPEN cur

FETCH NEXT FROM cur INTO @i

WHILE @@FETCH_STATUS=0
BEGIN
    IF (SELECT COUNT_BIG([TeamID])
        FROM TT
        WHERE [TeamID]=@i)>1
    BEGIN
        INSERT INTO @T
        SELECT TOP 1 *
        FROM [dbo].[TT]
        WHERE [TeamID]=@i AND [Assigned_to] IS NOT NULL
        ORDER BY [created_Date] DESC

    END
    ELSE
    BEGIN
        INSERT INTO @T
        SELECT *
        FROM [dbo].[TT]
        WHERE [TeamID]=@i

    END
    FETCH NEXT FROM cur INTO @i
END
SELECT TicketID
,      TeamID
,      StatusID
,      Assigned_to
,      created_Date
FROM @T
GROUP BY TicketID
,        TeamID
,        StatusID
,        created_Date
,        Assigned_to

CLOSE cur
DEALLOCATE cur

enter image description here