我每周都会收到以下示例SQL数据。
IF OBJECT_ID ('[tempdb].[dbo].[##GetSummaryColtSpotQusAns]') IS NOT NULL
DROP TABLE [dbo].[##GetSummaryColtSpotQusAns]
CREATE TABLE [dbo].[##GetSummaryColtSpotQusAns] (
[CourseWeek] [int] NULL
,[QuestionID] [int] NULL
,[QuestionGroupID] [int] NULL
,[QuestionType] [varchar](100) NULL
,[Question] [varchar](1000) NULL
,[Text] [varchar](1000) NULL
,[Value] [int] NULL
,[IsScore] [bit] NULL
,[AnswerScoreOrChoice] [int] NULL
)
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1083,1,'Label','Assess',NULL,NULL,NULL,NULL
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1084,1,'DropDown','Do you have any concerns?','No',2,1,NULL
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1084,1,'DropDown','Do you have any concerns?','Not Applicable',-1,1,NULL
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1084,1,'DropDown','Do you have any concerns?','Yes',1,1,1
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1085,1,'DropDown','Area Of Concern','Accuracy Of Scoring and Feedback',4,0,4
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1085,1,'DropDown','Area Of Concern','All',1,0,NULL
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1085,1,'DropDown','Area Of Concern','Course Access',2,0,2
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1085,1,'DropDown','Area Of Concern','Timely Submission',3,0,3
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1086,2,'Label','Coach',NULL,NULL,NULL,NULL
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1087,2,'DropDown','Do you have any concerns?','No',2,1,NULL
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1087,2,'DropDown','Do you have any concerns?','Not Applicable',-1,1,NULL
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1087,2,'DropDown','Do you have any concerns?','Yes',1,1,1
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1088,2,'DropDown','Area Of Concern','All',1,0,NULL
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1088,2,'DropDown','Area Of Concern','Communication',3,0,NULL
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1088,2,'DropDown','Area Of Concern','Rubric Misuse',2,0,NULL
INSERT INTO [dbo].[##GetSummaryColtSpotQusAns]
SELECT 1,1089,3,'Label','Engage',NULL,NULL,NULL,NULL
尝试枢轴生成低于预期的数据集数据,但是我没有获得预期的结果。
DECLARE @ExpectedResult TABLE (CourseWeek int, [Area Lable] varchar(100), [Concerns DDL] varchar(50), [Area DDL] varchar(500))
INSERT INTO @ExpectedResult VALUES (1 ,'Assess','Yes','Accuracy Of Scoring and Feedback,Course Access,Timely Submission')
INSERT INTO @ExpectedResult VALUES (1 ,'Coach','Yes',NULL)
INSERT INTO @ExpectedResult VALUES (1 ,'Engage',NULL,NULL)
SELECT * FROM @ExpectedResult
我试图获得与image / @ ExpectedResult表数据中突出显示的结果相似的结果。但是我正在获得其他NULL行。在删除[QuestionID]后部分解决了我的问题,但是仍然获得Null值,请在下面找到更新的Pivot查询。
SELECT [CourseWeek]
,[Area Reviewed]
,[Concerns DDL]
,[Area]
FROM (
SELECT DISTINCT [CourseWeek],
CASE
WHEN [QuestionGroupID] = 1
THEN 'Assess'
WHEN [QuestionGroupID] = 2
THEN 'Coach'
WHEN [QuestionGroupID] = 3
THEN 'Engage'
WHEN [QuestionGroupID] = 4
THEN 'Support'
END [Area Reviewed],
'Concerns DDL' AS [ConQuestionType],
'Area' AS [AreaQuestionType],
CASE
WHEN [AnswerScoreOrChoice] = 1
AND [Question] = 'Do you have any concerns?'
THEN 'Yes'
WHEN [AnswerScoreOrChoice] > 1
AND [Question] = 'Do you have any concerns?'
THEN 'No'
END [Concerns (Yes/No)],
AreaOfConcerns = (
STUFF((
SELECT ',' + DS2.[Text]
FROM [dbo].[##GetSummaryColtSpotQusAns] DS2
WHERE DS2.[QuestionID] = DS1.[QuestionID]
AND DS2.[QuestionType] = DS1.[QuestionType]
AND DS2.[CourseWeek] = DS1.[CourseWeek]
AND DS2.[Question] = 'Area Of Concern'
AND DS2.[Text] <> 'All'
FOR XML PATH(''),
TYPE
).value('.', 'varchar(max)'), 1, 1, '')
)
FROM [dbo].[##GetSummaryColtSpotQusAns] DS1
WHERE (
[AnswerScoreOrChoice] IS NOT NULL
OR [QuestionType] = 'Label'
)
) AS SRC
PIVOT(MAX([Concerns (Yes/No)]) FOR [ConQuestionType] IN ([Concerns DDL])) AS DES1
PIVOT(MAX(AreaOfConcerns) FOR [AreaQuestionType] IN ([Area])) AS DES2
答案 0 :(得分:0)
您遇到的问题是子查询中的QuestionID充当键字段,这意味着您的数据透视表将围绕Label围绕该字段旋转,以及LabelQuetionType等。并且由于它将为每一行提供一个不同的值,因此您的数据透视表将具有很多空值。
在子查询中删除QuestionId,您将看到它会按您的意愿进行旋转(尽管我认为您的示例数据在撰写本文时并不准确)。