动态Sql Pivot表的两列

时间:2018-06-25 17:54:00

标签: sql-server tsql

使用动态数据透视表,我尝试获取此表:http://www.sqlfiddle.com/#!18/9f1cf/47

看起来像这样:(为简便起见,删除了一些列,假设我可以在“选择委员会”之后有一个或多个列,这只是预期的设计,而不是预期的结果)

请注意,邮政编码可以为空,可以共享市议会,并且可以在几天内重复

+============+=======+================+============================+====================================+=========================+=====================+
| Call Date  |  Zip  | Chosen Council | Early Childhood Group Care | Development / Developmental Delays | Caregiver Mental Health | Behavioral Concerns |
+============+=======+================+============================+====================================+=========================+=====================+
| 2018-05-01 | 85000 | Maricopa North | null                       | 1                                  | 2                       | null                |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-01 | 85001 | Maricopa North | 1                          | null                               | null                    | null                |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-01 | null  | null           | null                       | 2                                  | null                    | null                |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-02 | 85000 | Maricopa North | null                       | 1                                  | 1                       | 3                   |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-02 | 85003 | Phoenix South  | null                       | null                               | null                    | 2                   |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-02 | 85004 | Phoenix South  | 1                          | 2                                  | null                    | 2                   |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-02 | null  | null           | null                       | 1                                  | 1                       | null                |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+

我已经看到了许多有关数据透视表的问题,无论是硬编码还是动态数据透视表,我仍然没有掌握。

在这里,我能够获得仅用于呼叫主题名称及其计数的动态数据透视表:http://www.sqlfiddle.com/#!18/9f1cf/39
但这只是所有内容的一行,它似乎也忽略了null。

在这里,我尝试对上述内容进行扩展,尽管看起来间隔更好,但我还没有弄清楚如何附加我的“致电日期”,“邮编”或“选择的理事会”列:http://www.sqlfiddle.com/#!18/9f1cf/37

有什么想法我该怎么做?


ASCII表使用:https://ozh.github.io/ascii-tables/

1 个答案:

答案 0 :(得分:1)

也许您需要以下类似内容

DECLARE @columns NVARCHAR(MAX), @sql NVARCHAR(MAX);
SET @columns = N'';
SELECT @columns += N', _callTopics.' + QUOTENAME(Name)
  FROM
  (
    SELECT
      _callTopics.Name
    FROM CallTopics AS _callTopics
    INNER JOIN CallTopicsPerRegion AS _callTopicsPerRegion
        ON _callTopics.Name = _callTopicsPerRegion.CallTopicName
    GROUP BY _callTopics.Name
  ) AS x;
SET @sql = N'
  SELECT CallDate
  ,Zip
  ,ChosenCouncil, ' + STUFF(@columns, 1, 2, '') + '
  FROM
  (
    SELECT _callTopics.Name, _callTopicsPerRegion.CallTopicCount,
    CallDate
  ,Zip
  ,ChosenCouncil
    FROM CallTopics AS _callTopics
    INNER JOIN CallTopicsPerRegion AS _callTopicsPerRegion
    ON _callTopics.Name = _callTopicsPerRegion.CallTopicName
  ) AS j
  PIVOT
  (
    SUM(CallTopicCount) FOR Name IN ('
    + STUFF(REPLACE(@columns, ', _callTopics.[', ',['), 1, 1, '')
    + ')
  ) AS _callTopics order by 1,2 ,3';
--PRINT @sql;
EXEC sp_executesql @sql;

这是 fiddle link