是我最好的选择吗?如何

时间:2018-07-13 15:34:31

标签: sql sql-server-2012

我具有以下表关系

Location.DeptFK
Dept.PK
Section.DeptFK
Subsection.SectionFK
Question.SubsectionFK
Answer.QuestionFK, SubmissionFK
Submission.PK, LocationFK

一个查询返回(MainTable)

QuestionNumberVar | Section | Subsection | Question                 | AnsYes | AnsNo | NA 
1-1.1               Math      Algebra      Did you do your homework?   10       1      1 
1-1.2               Math      Algebra      Did your dog eat it?        9        3      0
2-1.1               English   Greek        Did you do your homework?   8        0      4 

其他返回(Query2)

Answer | Location | QuestionNumberVar | Critical
1        High       1-1.1               1
2        Middle     1-1.1               1
2        High       1-1.2               0
1        Middle     1-1.2               0
0        High       2-1.1               1
1        Elem       2-1.1               1

我希望(Query2)返回(IndividualTable)

QuestionNumberVar | Critical | High | Middle | Ele
1-1.1                   1       1        2    'blank'
1-1.2                   0       2        1    'blank'
2-1.1                   1       0     'blank'   1

然后我想使用QuestionNumberVar作为排序键在上一张表的末尾合并它。所以桌子看起来像

  QuestionNumberVar | MainTable | IndividualTable
    1-1.1                Data         Data
    1-1.2                Data         Data
    2-1.1                Data         Data

答案按QuestionNumberVar分组的地方。 MainTable不是动态的,但其他IndividualTable需要动态的。在关系中,某些部门的位置并不总是出现。

这些查询用于收集所需的数据,但是我不知道如何将它们转换为如何修改我的表。我认为查询2应该使用Pivot来制作IndividualTable,我也不确定如何将IndividualTable与MainTable网格化

MainTable查询

SELECT        Section.StepNumber + '-' + Question.QuestionNumber AS QuestionNumberVar, 
            Question.Question, 
            Subsection.Name AS Subsection, 
            Section.Name AS Section, 
                     SUM(CASE WHEN (Answer.Answer = 0) THEN 1 ELSE 0 END) AS NA, 
                     SUM(CASE WHEN (Answer.Answer = 1) THEN 1 ELSE 0 END) AS AnsNo, 
                     SUM(CASE WHEN (Answer.Answer = 2) THEN 1 ELSE 0 END) AS AnsYes,
                     (select count(distinct Location.Abbreviation) from Department inner join Plant on location.DepartmentFK = Department.PK WHERE(Department.Name = 'insertParameter')) 
                    as total
FROM            Department inner join
section on Department.PK = section.DepartmentFK inner JOIN
subsection on Subsection.SectionFK = Section.PK INNER JOIN
question on Question.SubsectionFK = Subsection.PK INNER JOIN
Answer on Answer.QuestionFK = question.PK inner JOIN
Submission on Submission.PK = Answer.SubmissionFK inner join
 Location on Location.DepartmentFK = Department.PK AND Location.pk = Submission.PlantFK

WHERE        (Department.Name = 'InsertParameter') AND (Submission.MonthTested = '1/1/2017') 
GROUP BY Question.Question, QuestionNumberVar, Subsection.Name, Section.Name, Section.StepNumber
ORDER BY QuestionNumberVar;

查询2

SELECT  Answer.Answer, Location.abbreviation, Section.StepNumber + '-' + Question.QuestionNumber AS QuestionNumber, Cast(Question.CriticalProcessVariable AS VARCHAR) AS CriticalProcessVariable
FROM        Department left join
Section on Department.PK = Section.DepartmentFK left JOIN
Subsection on Subsection.SectionFK = Section.PK left JOIN
Question on Question.SubsectionFK = Subsection.PK left JOIN
Answer on Answer.QuestionFK = Question.PK left JOIN
Submission on Submission.PK = SubmissionFK left join
 Location on Location.DepartmentFK = Department.PK AND Location.pk = Submission.PlantFK 
 WHERE        (Department.Name = 'insertParameter') AND (Submission.MonthTested = '01/01/2017') 
ORDER BY CAST(Section.StepNumber as INT) ASC, Question.QuestionNumber;

我尝试将查询2旋转无济于事。我的问题总是由于无法通过“答案”求和而引起,因为它无法识别(可能是因为不在位置?),我有点迷失了,因为这是我做过的最复杂的查询,我无法包装我对Pivot的要求以及如何正确应用它有所了解。

1 个答案:

答案 0 :(得分:0)

Pivot是我处理此错误的最终方法,它一步一步地通过一个错误接一个错误。 T / SQL给我带来了一些麻烦。我首先是透视查询2

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(PivotQ.Abbreviation) 
                from ( --QUERY2 
                        ) PivotQ
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

set @query = 'SELECT StepNumber + ''-'' + QuestionNum as QuestionNumber, '+@cols+' from 
         (
            --Query2
        ) x
        pivot 
        (
            SUM(Answer)
            for Abbreviation in (' + @cols + ')
        ) p 
        '

然后,我使用QuestionNumber作为键来加入MainTable的QuestionNumber。我使用了第二个@cols来创建要在第二个查询中使用的列

select @cols2 = STUFF((SELECT distinct ', PivotJoin.' + QUOTENAME(PivotQ.Abbreviation) 
                from ( --QUERY2 
                        ) PivotQ
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

'+ @cols2 +' --added into Select of MainTable

-- Added to From Statement
inner join
('+ @query +') PivotJoin on PivotJoin.QuestionNumber = Section.StepNumber + ''-'' + Question.QuestionNumber

我不得不将Pivot的问题编号作为主要内容,因为现在它的T / SQL而不是使用Group By中的Alias的纯SQL不再起作用。然后,我在Group By子句中添加了@ cols2。并将其设置为Order By PivotJoin.QuestionNumber。