我有一个 MSSQL 数据库,它在一个表中包含一个人的生物信息:
ID: Name : Age : Sex
在另一个表格中,它保存了他们对以下许多问题的答案:
PersonID : QuestionID : Answer
是否可以通过 MSSQLMSE 将所有这些显示到一条记录中,如下所示:
ID : Name : Age : Sex : Question1Answer : Question2Answer : Question3Answer : And so on?
我有一个 MSSQL 数据库,它在一个表中包含一个人的生物信息:
ID: Name : Age : Sex
在另一个表格中,它保存了他们对以下许多问题的答案:
PersonID : QuestionID : Answer
是否可以通过 MSSQLMSE 将所有这些显示到一条记录中,如下所示:
ID : Name : Age : Sex : Question1Answer : Question2Answer : Question3Answer : And so on?
试试这个:
SELECT ID, name, age, sex, Question1answer, Question2answer
FROM
(
SELECT
p.Id,
p.Name,
p.Age,
p.sex,
questionanswer = 'Question' + CAST(q.questionid AS VARCHAR(10)) + 'answer',
q.Answer
FROM Persons p
INNER JOIN Questions q ON p.Id = q.UserID
) t
PIVOT
(
MAX(Answer)
FOR questionanswer IN([Question1answer], [Question2answer])
) p;
这会给你:
| ID | NAME | AGE | SEX | QUESTION1ANSWER | QUESTION2ANSWER |
-----------------------------------------------------------------
| 1 | Ahmed | 25 | M | Yes | No |
| 2 | Mohammed | 30 | M | No | Never |
| 3 | Sara | 25 | F | No | Never |
但是:如果您想为每个用户的任意数量的问题动态地执行此操作,您可以这样做:
DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);
select @cols = STUFF((SELECT distinct
',' +
QUOTENAME('Question' +
CAST(questionid AS VARCHAR(10)) +
'answer')
FROM questions
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
SET @query = 'SELECT ID, name, age, sex, ' + @cols +
' FROM
(
SELECT
p.Id,
p.Name,
p.Age,
p.sex,
questionanswer = ''Question'' +
CAST(q.questionid AS VARCHAR(10)) +
''answer'',
q.Answer
FROM Persons p
INNER JOIN Questions q ON p.Id = q.UserID
) t
PIVOT
(
MAX(Answer)
FOR questionanswer IN( ' + @cols + ') ) p ';
select ID,Name,Age,Sex,
(select top 1 Answer from Questions
where PersonId=prerson.id AND QuestionID=1) as Question1Answer,
(select top 1 Answer from Questions
where PersonId=prerson.id AND QuestionID=2) as Question2Answer,
(select top 1 Answer from Questions
where PersonId=prerson.id AND QuestionID=3) as Question3Answer
from prerson
sql server 中的 PIVOT 命令适合转置表结果,请参阅http://msdn.microsoft.com/en-us/library/ms177410(v=sql.100).aspx
下面的脚本适用于一组预定义的问题。对于可变数量的列,此链接将有助于http://blog-mstechnology.blogspot.com/2010/06/t-sql-pivot-operator-with-dynamic.html。
DECLARE @Person TABLE(
ID INT,
Name VARCHAR(50),
Age INT,
Sex CHAR(1)
)
DECLARE @Questions TABLE(
ID INT,
QuestionID INT,
Question VARCHAR(200),
Answer VARCHAR(200)
)
INSERT INTO @Person VALUES
(1,'Andrew',56,'M'),
(2,'Marge',65,'F')
INSERT INTO @Questions VALUES
(1,1,'Question1','Andrews Answer 1'),
(1,2,'Question2','Andrews Answer 2'),
(2,1,'Question1','Marge Answer 1'),
(2,3,'Question3','Marge Answer 3')
SELECT ID,Age,Name,Sex,
[1] AS 'Question1Answer',
[2] AS 'Question2Answer',
[3] AS 'Question3Answer'
FROM(
SELECT P.ID,P.Age,P.Name,P.Sex,Q.QuestionID,Q.Answer
FROM @Person P
INNER JOIN @Questions Q ON Q.ID = P.ID
) Source
PIVOT
(
MAX(Answer)
FOR QuestionID IN ([1],[2],[3])
)AS PT
试试这个:
SELECT
ID,
Name,
Age,
Sex,
[1] AS Question1Answer,
[2] AS Question1Answer,
[3] AS Question1Answer
FROM
(SELECT
i.ID,
i.Name,
i.Age,
i.Sex,
q.QuestionId,
q.Answer
FROM
dbo.Info AS i
INNER JOIN dbo.Questions AS q on q.ID = i.ID) AS m
PIVOT
(
MAX(Answer) FOR QuestionId in ([1],[2],[3])
) as PivotTable
我认为这(此处为 SQL Fiddle)是您正在寻找的。对于给定的人员 ID;
注意 p = Person Table
: a = Answer Table
和qId = QuestionId
DECLARE @S VARCHAR(Max)
DECLARE @PersonId INT = 1
SELECT @S=ISNULL(@S+' : ','') + convert(varchar(10), id) +' : '+ name
+' : '+ convert(varchar(10), age)+' : '+sex
FROM p
WHERE p.Id = @PersonId
SELECT @S=ISNULL(@S+' : ','') +
'Question' + convert(varchar(10), qid) +'Answer : '+ answer
FROM a
WHERE pId = @PersonId
SELECT @S
--Following results is in one line
--1 : X : 25 : M : Question11Answer : Here is my answer for 11 :
--Question12Answer : Here is my answer for 12 :
--Question101Answer : Here is my answer for 101
试试这个方法:(将数据库的兼容级别设置为90或更高)
begin
Declare @MainSQL nVarchar(Max)
Declare @Param nVarchar(Max)
Declare @QId int
set @Param = ''
Declare curParam Cursor For select Distinct QuestionID from Questions
open curParam
Fetch next from curParam into @QId
While @@Fetch_Status = 0
Begin
set @Param = @Param +'[' +convert(nVarchar,@QId ) + '],'
Fetch next from curParam into @QId
End
close curParam;
Deallocate curParam;
set @Param = left(@Param,len(@Param)-1)
Set @MainSQL = 'Select ID,Name,Age,***
From
(select P.ID,Name,Age,
Q.QuestionID, Q.Answer From Person P Left outer join
Questions Q
ON P.ID = Q.PersonID) S
Pivot
(Max(Answer)
For QuestionID in(***)
) as Pvt'
Set @MainSQL =replace(@MainSQL,'***',@Param);
--print @MainSQL
execute sp_executesql @MainSQL
End