2015-11-23 25 views
2

我都彼此相關的三個表如下加入SQL表和顯示的列行來生成報告

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 

解釋我想生成以下格式

是報告有一種使用T-SQL生成動態報告的方法,我嘗試使用數據透視表,但大多數互聯網上的示例只使用一個表和有限的字段。任何幫助或正確的方向將不勝感激。

回答

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 ('[email protected]+') 
    ) as piv 
'; 

-- execute the dynamic query 
--print @sql; 
Exec sp_executesql @sql; 

當你寫一個樞軸查詢,最好是使用子查詢(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') 
+0

感謝你的努力,但是我正在尋找一個動態解決方案,在您的解決方案中,問題是硬編碼的。但你至少已經向我展示了一個方向。歡呼聲 –

+0

看到我的動態SQL更新 –

+0

謝謝隊友,更新的答案是我一直在尋找 –

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 TABLE #Answers 
DROP TABLE #Contacts 
DROP TABLE #Questions