使用动态数据透视

时间:2016-10-13 18:21:21

标签: sql sql-server-2008 pivot

所以我正在研究一种方法来报告一些表格中的数据,该表格跟踪患者在访问急诊室时对胸痛问题的反应。当患者到达ER时,他们会被问到一系列关于胸痛的问题。对于每个问题,他们被要求将疼痛从1到10排名。以下是存储在表格中的数据。

+-----------+------------+----------+
| PatientId | QuestionId | PainRank |
+-----------+------------+----------+
|     1     |      1     |     5    |
+-----------+------------+----------+
|     1     |      2     |     6    |
+-----------+------------+----------+
|     1     |      3     |     4    |
+-----------+------------+----------+
|     2     |      1     |     1    |
+-----------+------------+----------+
|     2     |      2     |     8    |
+-----------+------------+----------+
|     2     |      3     |     2    |
+-----------+------------+----------+
|     3     |      1     |     5    |
+-----------+------------+----------+
|     3     |      2     |     4    |
+-----------+------------+----------+
|     3     |      3     |     7    |
+-----------+------------+----------+

我创建了一个动态的数据透视图,它将问题更改为列,以便医生可以查看患者组的摘要。

+-----------+---+---+---+
| PatientId | 1 | 2 | 3 |
+-----------+---+---+---+
|     1     | 5 | 6 | 4 |
+-----------+---+---+---+
|     2     | 1 | 8 | 2 |
+-----------+---+---+---+
|     3     | 5 | 4 | 7 |
+-----------+---+---+---+

我想要做的是在问题编号前加上"问题"所以列将看起来像#34;问题 - 1","问题-2","问题-3"。我想使用创建动态数据透视的SQL查询添加前缀,但由于数据透视是动态的,我不知道如何添加它。

这是我用来获取动态数据透视表的SQL。

  -- Dynamic Pivot
DECLARE @PivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnNameCollection AS NVARCHAR(MAX)

-- Get column list. A DISTINCT list of questions
SELECT @ColumnNameCollection= ISNULL(@ColumnNameCollection + ',','') 
       + QUOTENAME(QuestionId)
FROM (SELECT DISTINCT QuestionId FROM PatientChestPain) AS Questions

-- Build PIVOT query using the list of questions
SET @PivotQuery = 
  N'SELECT PatientId,  ' + @ColumnNameCollection + '
    FROM PatientChestPain
    PIVOT(MAX(PainRank) 
          FOR QuestionId IN (' + @ColumnNameCollection + ')) AS PivotTableResult'
--Execute the PIVOT query
EXEC sp_executesql @PivotQuery

我确实知道我可以编辑最终报告并添加"问题 - "但是我正在尝试自动生成报告,我只能操作SQL。

注意:我使用following website生成ascii表。非常好的界面和易于使用。我尝试创建一个SQL Fiddle,但它看起来好像目前无法使用SQL Server。

1 个答案:

答案 0 :(得分:3)

我找到了一个简单的方法。只需创建一个标题字符串,您可以在其中为每列创建别名

<强> DEMO

  -- Dynamic Pivot
DECLARE @PivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnNameCollection AS NVARCHAR(MAX)
DECLARE @HeaderNameCollection AS NVARCHAR(MAX)

-- Get column list. A DISTINCT list of Questions
SELECT @ColumnNameCollection= ISNULL(@ColumnNameCollection + ',','') 
       + QUOTENAME(QuestionId)
FROM (SELECT DISTINCT QuestionId FROM PatientChestPain) AS Questions

-- Get header list. A DISTINCT list of Questions
SELECT @HeaderNameCollection= ISNULL(@HeaderNameCollection + ',','') 
       + QUOTENAME(QuestionId) + ' as Q' + CAST(QuestionId AS VARCHAR(16))
FROM (SELECT DISTINCT QuestionId FROM PatientChestPain) AS Questions

SELECT @ColumnNameCollection;
SELECT @HeaderNameCollection;

-- Build PIVOT query using the list of Questions
SET @PivotQuery = 
  N'SELECT PatientId,  ' + @HeaderNameCollection + '
    FROM PatientChestPain
    PIVOT(MAX(PainRank) 
          FOR QuestionId IN (' + @ColumnNameCollection + ')) AS PivotTableResult'
--Execute the PIVOT query

SELECT @PivotQuery;
EXEC sp_executesql @PivotQuery

<强>输出

enter image description here