SQL将表组织成汇总视图

时间:2017-01-04 08:49:47

标签: sql sql-server-2008 tsql

我目前有一个类似于

的sql表
 +---------+--------------+------------+
| Company | Contact Type | Start Time |
+---------+--------------+------------+
| x       | Call         | 01/01/2016 |
| x       | Call         | 02/01/2016 |
| x       | Meeting      | 02/01/2016 |
| x       | Email        | 03/01/2016 |
| y       | Meeting      | 01/01/2016 |
| y       | Email        | 01/02/2016 |
| y       | Call         | 02/02/2016 |
| z       | Call         | 09/01/2016 |
| z       | Call         | 24/01/2016 |
| z       | Meeting      | 10/01/2016 |
| z       | Meeting      | 06/01/2016 |
+---------+--------------+------------+

我想要制作的是一个表格,它以下列格式显示最近的联系人类型:

+---------+------------+------------+------------+
| 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       |
+---------+------------+------------+------------+

现在我可以在SSRS中看起来像这样,并将数据集转换为交叉表矩阵报告,但我想在SQL中生成它。我还设法通过使用大量的子查询来生成这个,但这似乎不是一种有效的方法,而且真正的表有数百万行和更多的联系类型,而不是调用,会议和电子邮件。所以问题是,生成第二个表的最有效方法是什么。为了论证,我们可以说提取表1的sql代码是:

SELECT Company, [Contact Type], [Start Time] From Db.dbo.History

3 个答案:

答案 0 :(得分:2)

您也可以这样做:(使用PIVOT)

-- Your Sample
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

My result

答案 1 :(得分:1)

GROUP BY。使用case表达式进行条件聚合:

SELECT Company,
       max(case when [Contact Type] = 'Call' then [Start Time] end) [Call],
       max(case when [Contact Type] = 'Meeting' then [Start Time] end) [Meeting],
       max(case when [Contact Type] = 'Email' then [Start Time] end) [Email]
From Db.dbo.History
group by Company

答案 2 :(得分:0)

您可以在此处使用CASE:

SELECT Company
    ,CASE [Contact Type] WHEN 'Call' THEN  MAX([Start Time]) END AS [Call]
    ,CASE [Contact Type] WHEN 'Meeting' THEN  MAX([Start Time]) END AS  [Meeting]
    ,CASE [Contact Type] WHEN 'Email' THEN  MAX([Start Time]) END AS    [Email]
From Db.dbo.History
GROUP BY Company