SQL Pivot函数获取前注释字段

时间:2017-01-05 15:10:41

标签: sql sql-server sql-server-2008 tsql pivot

请参阅此问题以获取更多背景信息:question 1我使用下面的代码使用pivot功能创建了一个信息列表

  WITH List AS
    (
        SELECT 'x' AS Company, 'Call'    AS ContactType, '01/01/2016' AS StartTime UNION ALL
        SELECT 'x' AS Company, 'Call'    AS ContactType, '02/01/2016' AS StartTime UNION ALL
        SELECT 'x' AS Company, 'Meeting' AS ContactType, '02/01/2016' AS StartTime UNION ALL
        SELECT 'x' AS Company, 'Email'   AS ContactType, '03/01/2016' AS StartTime UNION ALL
        SELECT 'y' AS Company, 'Meeting' AS ContactType, '01/01/2016' AS StartTime UNION ALL
        SELECT 'y' AS Company, 'Email'   AS ContactType, '01/02/2016' AS StartTime UNION ALL
        SELECT 'y' AS Company, 'Call'    AS ContactType, '02/02/2016' AS StartTime UNION ALL
        SELECT 'z' AS Company, 'Call'    AS ContactType, '09/01/2016' AS StartTime UNION ALL
        SELECT 'z' AS Company, 'Call'    AS ContactType, '24/01/2016' AS StartTime UNION ALL
        SELECT 'z' AS Company, 'Meeting' AS ContactType, '10/01/2016' AS StartTime UNION ALL
        SELECT 'z' AS Company, 'Meeting' AS ContactType, '06/01/2016' AS StartTime 
    )


    SELECT Company, Call, Meeting, Email
    FROM
    (
      SELECT StartTime, ContactType, Company
        FROM List
    ) L
    PIVOT
    (
      MAX(StartTime)
      FOR ContactType IN (Call, Meeting, Email)
    ) CT

这有助于产生表格结果:

+---------+------------+------------+------------+
| Company |    Call    |  Meeting   |   Email    |
+---------+------------+------------+------------+
| x       | 02/01/2016 | 02/01/2016 | 03/01/2016 |
| y       | 02/02/2016 | 01/01/2016 | 01/02/2016 |
| z       | 24/01/2016 | 10/01/2016 | Null       |
+---------+------------+------------+------------+

我想通过在我的原始查询中加入一个额外的列来进一步解决这个问题。我想在查询中注明如下:

WITH List AS
    (
        SELECT 'x' AS Company, 'Call'    AS ContactType, '01/01/2016' AS StartTime, 'blablabla1' as Note UNION ALL
        SELECT 'x' AS Company, 'Call'    AS ContactType, '02/01/2016' AS StartTime, 'blablabla2' as Note UNION ALL
        SELECT 'x' AS Company, 'Meeting' AS ContactType, '02/01/2016' AS StartTime, 'blablabla3' as Note UNION ALL
        SELECT 'x' AS Company, 'Email'   AS ContactType, '03/01/2016' AS StartTime, 'blablabla4' as Note UNION ALL
        SELECT 'y' AS Company, 'Meeting' AS ContactType, '01/01/2016' AS StartTime, 'blablabla5' as Note UNION ALL
        SELECT 'y' AS Company, 'Email'   AS ContactType, '01/02/2016' AS StartTime, 'blablabla6' as Note UNION ALL
        SELECT 'y' AS Company, 'Call'    AS ContactType, '02/02/2016' AS StartTime, 'blablabla7' as Note UNION ALL
        SELECT 'z' AS Company, 'Call'    AS ContactType, '09/01/2016' AS StartTime, 'blablabla8' as Note UNION ALL
        SELECT 'z' AS Company, 'Call'    AS ContactType, '24/01/2016' AS StartTime, 'blablabla9' as Note UNION ALL
        SELECT 'z' AS Company, 'Meeting' AS ContactType, '10/01/2016' AS StartTime, 'blablabla10' as Note UNION ALL
        SELECT 'z' AS Company, 'Meeting' AS ContactType, '06/01/2016' AS StartTime, 'blablabla11' as Note 
    )

我想要生成的表输出是:

+---------+------------+------------+------------+------------+
| Company |    Call    |  Meeting   |   Email    |    Note    |
+---------+------------+------------+------------+------------+
| x       | 02/01/2016 | 02/01/2016 | 03/01/2016 | blablabla4 |
| y       | 02/02/2016 | 01/01/2016 | 01/02/2016 | blablabla7 |
| z       | 24/01/2016 | 10/01/2016 | Null       | blablabla9 |
+---------+------------+------------+------------+------------+

现在是棘手的一点。我希望根据联系方式选择的笔记是最新的笔记。在第一行注释:blablabla4被选中,因为最近的联系是通过电子邮件' 03/01/2016'因此它选择了这个音符。

有没有人知道如何在不使用子查询我自己的数据的情况下实现这一目标。

2 个答案:

答案 0 :(得分:3)

使用Conditional Aggregate代替枢轴

WITH List AS
    (
        SELECT 'x' AS Company, 'Call'    AS ContactType, '01/01/2016' AS StartTime, 'blablabla1' as Note UNION ALL
        SELECT 'x' AS Company, 'Call'    AS ContactType, '02/01/2016' AS StartTime, 'blablabla2' as Note UNION ALL
        SELECT 'x' AS Company, 'Meeting' AS ContactType, '02/01/2016' AS StartTime, 'blablabla3' as Note UNION ALL
        SELECT 'x' AS Company, 'Email'   AS ContactType, '03/01/2016' AS StartTime, 'blablabla4' as Note UNION ALL
        SELECT 'y' AS Company, 'Meeting' AS ContactType, '01/01/2016' AS StartTime, 'blablabla5' as Note UNION ALL
        SELECT 'y' AS Company, 'Email'   AS ContactType, '01/02/2016' AS StartTime, 'blablabla6' as Note UNION ALL
        SELECT 'y' AS Company, 'Call'    AS ContactType, '02/02/2016' AS StartTime, 'blablabla7' as Note UNION ALL
        SELECT 'z' AS Company, 'Call'    AS ContactType, '09/01/2016' AS StartTime, 'blablabla8' as Note UNION ALL
        SELECT 'z' AS Company, 'Call'    AS ContactType, '24/01/2016' AS StartTime, 'blablabla9' as Note UNION ALL
        SELECT 'z' AS Company, 'Meeting' AS ContactType, '10/01/2016' AS StartTime, 'blablabla10' as Note UNION ALL
        SELECT 'z' AS Company, 'Meeting' AS ContactType, '06/01/2016' AS StartTime, 'blablabla11' as Note 
    )   
SELECT Company,
       call = Max(CASE WHEN ContactType = 'call' THEN StartTime END),
       Meeting = Max(CASE WHEN ContactType = 'Meeting' THEN StartTime END),
       Email = Max(CASE WHEN ContactType = 'Email' THEN StartTime END),
       Note
FROM   (SELECT Company,
               ContactType,
               StartTime,
               First_value(Note)OVER(partition BY Company ORDER BY StartTime DESC) AS Note
        FROM   list) a
GROUP  BY Company,note 

答案 1 :(得分:3)

您可以使用ROW_NUMBER()获取最新音符

WITH List AS
    (
        SELECT 'x' AS Company, 'Call'    AS ContactType, '01/01/2016' AS StartTime, 'blablabla1' as Note UNION ALL
        SELECT 'x' AS Company, 'Call'    AS ContactType, '02/01/2016' AS StartTime, 'blablabla2' as Note UNION ALL
        SELECT 'x' AS Company, 'Meeting' AS ContactType, '02/01/2016' AS StartTime, 'blablabla3' as Note UNION ALL
        SELECT 'x' AS Company, 'Email'   AS ContactType, '03/01/2016' AS StartTime, 'blablabla4' as Note UNION ALL
        SELECT 'y' AS Company, 'Meeting' AS ContactType, '01/01/2016' AS StartTime, 'blablabla5' as Note UNION ALL
        SELECT 'y' AS Company, 'Email'   AS ContactType, '01/02/2016' AS StartTime, 'blablabla6' as Note UNION ALL
        SELECT 'y' AS Company, 'Call'    AS ContactType, '02/02/2016' AS StartTime, 'blablabla7' as Note UNION ALL
        SELECT 'z' AS Company, 'Call'    AS ContactType, '09/01/2016' AS StartTime, 'blablabla8' as Note UNION ALL
        SELECT 'z' AS Company, 'Call'    AS ContactType, '24/01/2016' AS StartTime, 'blablabla9' as Note UNION ALL
        SELECT 'z' AS Company, 'Meeting' AS ContactType, '10/01/2016' AS StartTime, 'blablabla10' as Note UNION ALL
        SELECT 'z' AS Company, 'Meeting' AS ContactType, '06/01/2016' AS StartTime, 'blablabla11' as Note 
    )
Select Company
      ,Call    = max(case when ContactType='Call'    then StartTime end)
      ,Meeting = max(case when ContactType='Meeting' then StartTime end)
      ,Email   = max(case when ContactType='Email'   then StartTime end)
      ,Note    = max(case when ContactType<>'Email' and RN=1 then Note end)
 From (
        Select *
              ,RN = Row_Number() over (Partition By Company,case when ContactType <>'EMail' then 1 else 0 end Order By StartTime Desc) 
         from List
      ) A
 Group BY Company

返回

Company Call        Meeting     Email       Note
x       02/01/2016  02/01/2016  03/01/2016  blablabla3
y       02/02/2016  01/01/2016  01/02/2016  blablabla7
z       24/01/2016  10/01/2016  NULL        blablabla9