我有三个表 - 第一个包含具有唯一ID的问题
QID | Question
------------------
1 | Question 1
2 | Question 2
3 | Question 3
4 | Question 4
5 | Question 5
第二个包含这些问题的答案,第一个表中的唯一ID,然后是将答案链接在一起的记录的ID
QID | Answer | Parent
-------------------------
1 | Yes | 123
2 | No | 123
3 | No | 123
4 | Yes | 123
5 | Yes | 123
1 | No | 124
2 | Yes | 124
3 | No | 124
4 | No | 124
5 | No | 124
第三部分包含有关父母的其他信息
Parent | Parent Title
----------------------------------------
123 | Questionnaire Sample 1
124 | Questionnaire Sample 2
理想情况下,我想创建一个视图,将数据转换或转换为更有用的格式,我想的是:
Parent | Question 1 | Question 2 | Question 3 | Question 4 | Question 5
123 | Yes | No | No | Yes | Yes
124 | No | Yes | No | No | No
我在这里发现了一些非常好的线程,我一直在使用:
Simple way to transpose columns and rows in Sql?
T-SQL Pivot/Unpivot(Transpose) Column Headers Needed as Data Rows
还有一个:
Transpose rows into columns in SQL Server 2008 R2
我从以下方面提取了以下内容:
SELECT [First_Set], [Second_Set]
FROM
(
SELECT B.ColumnName, A.Value
, row_number() over(partition by a.Table2ID
order by a.Value) seq
FROM Table_1 AS A
INNER JOIN Table_2 AS B
ON A.Table2ID = B.ID
) AS P
PIVOT
(
min(P.Value)
for P.ColumnName in ([First_Set], [Second_Set])
) AS PIV;
然后我将其修改为:
SELECT [PARENT], [QUESTION], [ANSWER]
FROM
(
SELECT B.PARENT_TITLE, C.QUESTION, A.ANSWER
, row_number() over(partition by a.PARENT
order by a.PARENT) seq
FROM answerTable AS A
INNER JOIN parentTable AS B
ON A.PARENT = B.PARENT
INNER JOIN questionTable AS C
ON A.QID = C.QID
) AS P
PIVOT
(
min(P.RESULT)
for P.PARENT in ([PARENT], [QUESTION], [ANSWER])
) AS PIV;
返回但仍然不是我需要它。
创建新表格对我来说是不可行的,所以理想情况下,我在这里寻找一种动态的方法,它是报告所以我认为视图最简单/最好但是我我愿意接受建议。
感谢。
答案 0 :(得分:2)
以下是动态执行的方法:
create table #t1(QID int, Question char(10))
insert #t1 values
(1, 'Question 1'),
(2, 'Question 2'),
(3, 'Question 3'),
(4, 'Question 4'),
(5, 'Question 5')
create table #t2 (QID int, Answer char(3), Parent int)
insert #t2 values
(1, 'Yes', 123),
(2, 'No ', 123),
(3, 'No ', 123),
(4, 'Yes', 123),
(5, 'Yes', 123),
(1, 'No ', 124),
(2, 'Yes', 124),
(3, 'No ', 124),
(4, 'No ', 124),
(5, 'No ', 124)
declare @collist nvarchar(max)
SET @collist = stuff((select distinct ',' + QUOTENAME(Question)
FROM #t1 -- your table here
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select @collist
declare @q nvarchar(max)
set @q = '
select *
from (
select
Question, Answer, Parent
from (
select #t1.*, #t2.Answer, #t2.parent
from #t1
inner join #t2 on #t1.QID = #t2.QID
) as x
) as source
pivot (
max(Answer)
for Question in (' + @collist + ')
) as pvt
'
exec (@q)
答案 1 :(得分:0)
解决问题的另一种方法 -
declare @dynamicSQL varchar(max) = ''
declare @pivotList varchar(max) = ''
declare @uniqueQuestions table ([Question] varchar(50) )
insert into @uniqueQuestions
select distinct [Question] from Questions
select @pivotList = @pivotList + '[' + Question + '],' from @uniqueQuestions
set @pivotList = SUBSTRING(@pivotList, 0, LEN(@pivotList))
set @dynamicSQL =
'select *
from
(
select Parent, Question, Answer
from Questions as q
left join Answers as a
on q.QID = a.QID
) as src
pivot(
max(answer)
for Question in (' + @pivotList + ')' +
')as pvt'
exec(@dynamicSQL)
感谢这篇文章的实现 - Convert multiple rows into one with comma as separator