我有三个相互关联的表格,如下所述
Contacts
====================================================================================
ContactID ContactName
1 Contact 1
2 Contact 2
3 Contact 3
4 Contact 4
5 Contact 5
Questions
=====================================================================================
QuestionID QuestionText
1 What is your Department?
2 Would you be interested in attending this year show?
3 Number of Employees in your company?
Answers
======================================================================================
QuestionID ContactID AnswerText
1 1 IT
1 2 HR
3 4 60
2 2 Yes
我想以下面的格式生成报告
Report
====================================================================================================================================================
ContactID What is your Department? Would you be interested in attending this year show? Number of Employees in your company?
1 IT NULL NULL
2 HR Yes NULL
4 NULL NULL 60
有没有办法使用T-SQL生成动态报告,我尝试使用数据透视表,但互联网上的大部分样本只使用一个表和有限的字段。任何帮助或正确的方向将不胜感激。
答案 0 :(得分:1)
您可以使用此查询:
-- you columns
Declare @cols nvarchar(max);
Set @cols = STUFF((Select distinct ', ' + QUOTENAME(QuestionText)
From #Questions FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
-- dynamic query
Declare @sql nvarchar(max);
Set @sql = '
Select *
From (
Select a.ContactID, a.AnswerText, q.QuestionText From #Questions q
Inner Join #Answers a On a.QuestionID = q.QuestionID
) as x
Pivot (
MAX(AnswerText)
For QuestionText in ('+@cols+')
) as piv
';
-- execute the dynamic query
--print @sql;
Exec sp_executesql @sql;
编写Pivot查询时,最好使用子查询(此处为X)或CTE。这将用于连接您的数据并仅返回数据透视表所需的3行。
@cols用于获取列的列表:[Number of Employees in your company?], [What is your Department?], [Would you be interested in attending this year show?]
这被添加到@sql变量中。它包含将使用sp_executesql
执行的主要查询。
<强>输出:强>
ContactID What is your Department? Would you be interested in attending this year show? Number of Employees in your company?
1 IT NULL NULL
2 HR Yes NULL
4 NULL NULL 60
您的数据:
Declare table #Questions(QuestionID int, QuestionText varchar(100))
Insert into @Questions(QuestionID, QuestionText) values
(1, 'What is your Department?')
, (2, 'Would you be interested in attending this year show?')
, (3, 'Number of Employees in your company?')
Declare table #Answers(QuestionID int, ContactID int, AnswerText varchar(10))
Insert into @Answers(QuestionID, ContactID, AnswerText) values
(1, 1, 'IT')
, (1, 2, 'HR')
, (3, 4, '60')
, (2, 2, 'Yes')
答案 1 :(得分:1)
我已经在link的帮助下动态创建了列名。添加了查询的内嵌注释
-- Declare Variables
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
-- Get distinct values of the PIVOT Column, here QuestionText
SELECT @ColumnName = ISNULL(@ColumnName + ',','') + QUOTENAME(QuestionText)
FROM (SELECT DISTINCT QuestionText FROM #Questions) AS QText
-- SELECT @ColumnName
SET @DynamicPivotQuery = N'
SELECT *
FROM (
SELECT ANS.ContactID, ANS.AnswerText, QUE.QuestionText
FROM #Questions QUE
INNER JOIN #Answers ANS ON ANS.QuestionID = QUE.QuestionID
) AS T
PIVOT (
MAX(AnswerText)
FOR QuestionText IN (' + @ColumnName + ')
) AS PVTTable'
-- Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery
临时表创建是:
CREATE TABLE #Contacts(ContactID INT, ContactName VARCHAR (50))
CREATE TABLE #Questions( QuestionID INT, QuestionText VARCHAR(100))
CREATE TABLE #Answers(QuestionID INT, ContactID INT, AnswerText VARCHAR (100))
INSERT INTO #Contacts
SELECT 1, 'Contact 1' UNION SELECT
2, 'Contact 2' UNION SELECT
3, 'Contact 3' UNION SELECT
4, 'Contact 4' UNION SELECT
5, 'Contact 5'
INSERT INTO #Questions
SELECT 1 , 'What is your Department?' UNION SELECT
2 , 'Would you be interested in attending this year show?' UNION SELECT
3 , 'Number of Employees in your company?'
INSERT INTO #Answers
SELECT 1, 1 , 'IT' UNION SELECT
1, 2 , 'HR' UNION SELECT
3, 4 , '60' UNION SELECT
2, 2 , 'Yes'
DROP临时表:
DROP TABLE #Answers
DROP TABLE #Contacts
DROP TABLE #Questions