返回每列

时间:2017-09-07 16:55:59

标签: sql sql-server

样本表

+--------+------------+------------------+-------------+-------------+
| FileID |    Date    |     Activity     | Assigned_By | Responsible |
+--------+------------+------------------+-------------+-------------+
|    123 | 2016/01/01 | Work in progress | Foo1        | Bob         | 
|    234 | 2016/01/01 | Work in progress | Foo2        | Smith       | 
|    123 | 2016/01/02 | Escalated        | NULL        | NULL        | 
|    123 | 2016/01/03 | Need reassign    | NULL        | NULL        | 
|    123 | 2016/01/03 | Reassigned       | Foo2        | John        | 
|    234 | 2016/01/03 | Completed        | NULL        | NULL        |
|    123 | 2016/01/04 | Completed        | NULL        | NULL        |
+--------+------------+------------------+-------------+-------------+

我的查询:

SELECT FileID,
       Date,
       Activity,
       Assigned_By,
       Responsible
FROM (
      SELECT fooTable.*, ROW_NUMBER() OVER (PARTITION BY FileID ORDER BY Date DESC) AS Separator
     ) fooTable
INNER JOIN randomTable ON fooTable.FileID = randomTable.ID
WHERE fooTable.Separator = 1;

返回:

+--------+------------+-----------+-------------+-------------+
| FileID |    Date    | Activity  | Assigned By | Responsible |
+--------+------------+-----------+-------------+-------------+
|    234 | 2016/01/03 | Completed | NULL        | NULL        |
|    123 | 2016/01/04 | Completed | NULL        | NULL        |
+--------+------------+-----------+-------------+-------------+

所需结果 - 使用最新日期返回每行唯一FileID的LATEST列记录的每一行:

+--------+------------+-----------+-------------+-------------+
| FileID |    Date    | Activity  | Assigned By | Responsible |
+--------+------------+-----------+-------------+-------------+
|    234 | 2016/01/03 | Completed | Foo2        | John        |
|    123 | 2016/01/04 | Completed | Foo1        | Bob         |
+--------+------------+-----------+-------------+-------------+

我理解为什么查询不起作用,因为它只返回最新的行(由row_number分配1),因此我将根据降序日期收到该唯一FileID的第一条ROW记录。但我不知道如何解决它。

编辑:我意识到的其他事情是MAX()不适用于Assigned_By和Responsible(我认为),因为它会返回更大的字母名称......

2 个答案:

答案 0 :(得分:2)

您使用联接或您可以使用FIRST_VALUE,如下所示:

SELECT 
  FileID,
  FIRST_VALUE(Date) OVER (PARTITION BY FileID ORDER BY Date DESC ROWS  BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS Date,
  FIRST_VALUE(Activity) OVER (PARTITION BY FileID ORDER BY CASE WHEN Activity IS NULL THEN 0 ELSE 1 END DESC, Date DESC ROWS  BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS Activity,
  FIRST_VALUE(Assigned_By) OVER (PARTITION BY FileID ORDER BY CASE WHEN Assigned_By IS NULL THEN 0 ELSE 1 END DESC, Date DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS Assigned_By,
  FIRST_VALUE(Responsible) OVER (PARTITION BY FileID ORDER BY CASE WHEN Responsible IS NULL THEN 0 ELSE 1 END DESC, Date DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS Responsible
FROM fooTable
INNER JOIN randomTable ON fooTable.FileID = randomTable.ID
WHERE fooTable.Separator = 1;     

答案 1 :(得分:2)

您可以使用条件聚合执行您想要的操作:

WITH t AS (
      SELECT FileID, Date, Activity, Assigned_By, Responsible
      FROM fooTable INNER JOIN 
           randomTable
           ON fooTable.FileID = randomTable.ID
     )
SELECT FileID, MAX(Date) as date,
       MAX(CASE WHEN seqnum = 1 THEN Activity END) as Activity,
       MAX(CASE WHEN seqnum_nonnull = 1 THEN Assigned_By END) as Assigned_By,
       MAX(CASE WHEN seqnum_nonnull = 1 THEN Responsible END) as Responsible
FROM (SELECT t.*,
             ROW_NUMBER() OVER (PARTITION BY FileID ORDER BY Date DESC) AS seqnum,
             ROW_NUMBER() OVER (PARTITION BY FileID
                                ORDER BY (CASE WHEN AssignedBy IS NOT NULL THEN 1 ELSE 2 END), Date DESC
                               ) AS seqnum_notnull
      FROM t
     ) t
GROUP BY FileID;