SQL查询返回两个表的报告

时间:2012-11-29 11:49:29

标签: sql sql-server-2008 tsql pivot

我有两个表AnswersUsers,如下所示:

Answers (AnswerID, QuestionNumber, Answer, UserID)
Users (UserID, FirstName, LastName)

我可以运行什么SQL查询来返回这样的报告:

enter image description here

2 个答案:

答案 0 :(得分:3)

您可以使用PIVOT函数转换此数据。您将Name(名字/姓氏)作为标题似乎很不寻常,但如果这是您想要的,那么您可以使用以下内容:

select *
from
(
  select a.questionnumber,
    u.firstname +' '+ u.lastname as Name,
    a.answer
  from answers a
  left join users u
    on a.userid = u.userid
) src
pivot
(
  max(answer)
  for name in ([John Smith], [Bob Jones])
) piv;

请参阅SQL Fiddle with Demo

结果:

| QUESTIONNUMBER | JOHN SMITH | BOB JONES |
-------------------------------------------
|              1 |       blah |      test |
|              2 |      hsdfk |    (null) |

我认为你会希望Question Numbers作为标题,下面的答案如下:

select *
from
(
  select 'QuestionNumber'+cast(a.questionnumber as varchar(10)) questionnumber,
    u.firstname +' '+ u.lastname as Name,
    a.answer
  from answers a
  left join users u
    on a.userid = u.userid
) src
pivot
(
  max(answer)
  for questionnumber in ([QuestionNumber1], [QuestionNumber2])
) piv;

请参阅SQL Fiddle with Demo

结果:

|       NAME | QUESTIONNUMBER1 | QUESTIONNUMBER2 |
--------------------------------------------------
|  Bob Jones |            test |          (null) |
| John Smith |            blah |           hsdfk |

如果您要转换已知数量的项目,上述答案将会很有效。但是如果您有一个未知的数字,那么您可以使用动态SQL来PIVOT数据:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME('QuestionNumber'+cast(questionnumber as varchar(10))) 
                    from answers
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT Name, ' + @cols + ' from 
             (
              select ''QuestionNumber''+cast(a.questionnumber as varchar(10)) questionnumber,
                u.firstname +'' ''+ u.lastname as Name,
                a.answer
              from answers a
              left join users u
                on a.userid = u.userid
            ) x
            pivot 
            (
                max(answer)
                for questionnumber in (' + @cols + ')
            ) p '

execute(@query)

请参阅SQL Fiddle with Demo

结果:

|       NAME | QUESTIONNUMBER1 | QUESTIONNUMBER2 |
--------------------------------------------------
|  Bob Jones |            test |          (null) |
| John Smith |            blah |           hsdfk |

答案 1 :(得分:0)

这称为交叉表查询 - 有大量关于如何生成它们的示例;例如:

http://www.simple-talk.com/sql/t-sql-programming/creating-cross-tab-queries-and-pivot-tables-in-sql/

修改

使用pivot的@Bluefleet可能是最好的答案;使用简单的交叉表可能需要您事先了解数据形状的某些方面,因此列本质上是硬编码的 - 如果列是稳定集,则以下内容应该有效:

declare @Answers table (AnswerID int, QuestionNumber int , Answer int , UserID int)
declare @Users table (UserID int , FirstName varchar(10), LastName varchar(10))
declare @Questions table (QuestionNumber int, QuestionName varchar(10))

insert into @Answers (AnswerID, QuestionNumber, Answer, UserID) values (1, 1, 10, 1)
insert into @Answers (AnswerID, QuestionNumber, Answer, UserID) values (2, 2, 20, 1)
insert into @Answers (AnswerID, QuestionNumber, Answer, UserID) values (3, 3, 30, 2)
insert into @Answers (AnswerID, QuestionNumber, Answer, UserID) values (1, 1, 40, 2)
insert into @Answers (AnswerID, QuestionNumber, Answer, UserID) values (2, 2, 50, 2)
insert into @Answers (AnswerID, QuestionNumber, Answer, UserID) values (3, 3, 60, 3)
insert into @Answers (AnswerID, QuestionNumber, Answer, UserID) values (1, 1, 70, 3)
insert into @Answers (AnswerID, QuestionNumber, Answer, UserID) values (2, 2, 80, 3)

insert into @Users (UserID, FirstName, LastName) values (1, 'Tom', 'Smith')
insert into @Users (UserID, FirstName, LastName) values (2, 'Dick', 'Brown')
insert into @Users (UserID, FirstName, LastName) values (3, 'Harry', 'Robinson')

insert into @Questions (QuestionNumber, QuestionName) values (1, 'Question 1')
insert into @Questions (QuestionNumber, QuestionName) values (2, 'Question 2')
insert into @Questions (QuestionNumber, QuestionName) values (3, 'Question 3')

select 
    QuestionName, MAX(User1) as 'Tom Smith', MAX(User2) as 'Dick Brown', MAX(User3) as 'Harry Robinson'
from 
(
select 
    q.QuestionName,
    (case when a.UserID=1 then Answer else null end) as 'User1',
    (case when a.UserID=2 then Answer else null end) as 'User2',
    (case when a.UserID=3 then Answer else null end) as 'User3'
from @Answers a
join @Users u on u.UserID = a.UserID
join @Questions q on q.QuestionNumber = a.QuestionNumber
) as combined
group by QuestionName



QuestionName Tom Smith   Dick Brown  Harry Robinson
------------ ----------- ----------- --------------
Question 1   10          40          70
Question 2   20          50          80
Question 3   NULL        30          60