我对sql查询有疑问。假设我们有一个表
报告:
+----+-------------+---------+--------+
| ID | reporter_id | subject | report |
+----+-------------+---------+--------+
| 1 | 1 | A | "OK" |
| 2 | 1 | B | "OK" |
| 3 | 1 | c |"NOT OK"|
| 4 | 2 | A | "OK" |
| 5 | 2 | C | "OK" |
+----+-------------+---------+--------+
现在,我想要做的是为每个reporter_id选择所有报告并生成下表:
+-------------+-----------------+-----------------+-----------------+
| reporter_id | report_subjectA | report_subjectB | report_subjectC |
+-------------+-----------------+-----------------+-----------------+
| 1 | "OK" | "OK" | "NOT OK" |
| 2 | "OK" | NULL | "OK" |
+-------------+-----------------+-----------------+-----------------+
有可能吗?
谢谢
答案 0 :(得分:1)
如果受试者无限制,请使用:
SELECT * INTO #src FROM
(VALUES
(1, 1, 'A', 'OK' ),
(2, 1, 'B', 'OK' ),
(3, 1, 'C', 'NOT OK'),
(4, 2, 'A', 'OK' ),
(5, 2, 'C', 'OK' )
)T(ID, reporter_id, subject, report)
DECLARE @columns nvarchar(MAX) = STUFF((
SELECT DISTINCT ',[Report Subject '+subject+']'
FROM #Src
FOR XML PATH('')), 1, 1, '')
DECLARE @sql nvarchar(MAX) = N'
SELECT * FROM
(
SELECT reporter_id, ''Report Subject ''+subject Title, report
FROM #Src
) T
PIVOT
(MAX(report) FOR Title IN ('+@columns+')) P'
EXEC (@sql)
如果只有3,请使用简化的PIVOT
:
SELECT * FROM
(
SELECT reporter_id, 'Report Subject '+subject Title, report
FROM #Src
) T
PIVOT
(MAX(report) FOR Title IN ([Report Subject A], [Report Subject B], [Report Subject C])) P
两者都返回:
reporter_id Report Subject A Report Subject B Report Subject C
----------- ---------------- ---------------- ----------------
1 OK OK NOT OK
2 OK NULL OK
答案 1 :(得分:0)
试试这个:
declare @id int,@a char(10),@b char(10),@c char(10)
declare @reports table (reporter_id int, report_subjectA char(10), report_subjectB char(10), report_subjectC char(10))
declare c cursor for
select distinct reporter_id from reports
open c
fetch next from c into @id
while @@FETCH_STATUS=0
begin
set @a=NULL
set @B=NULL
set @c=NULL
if exists(select report from reports where subjects='A' and reporter_id =@id) set @a=(select report from reports where subjects='A' and reporter_id =@id)
if exists (select report from reports where subjects='B' and reporter_id =@id) set @b=(select report from reports where subjects='B' and reporter_id =@id)
if exists (select report from reports where subjects='C' and reporter_id =@id) set @c=(select report from reports where subjects='C' and reporter_id =@id)
insert into @reports values (@id,@a,@b,@c)
fetch next from c into @id
end
close c
deallocate c
select * from @reports
答案 2 :(得分:0)
如果只有3个科目。
Select reporter_id, report_subjectA, report_subjectB, report_subjectC from
(
Select reporter_id, report, rep_sub = 'report_subject' + [Subject] from TableReports r
) SourceT
Pivot
(
max(report) for rep_sub in ([report_subjectA],[report_subjectB],[report_subjectC])
) AS PivotedT
答案 3 :(得分:0)
您可以如下所示:
SELECT
reporter_id,
MIN(CASE WHEN UPPER([subject]) = 'A' THEN report ELSE NULL END) report_subjectA,
MIN(CASE WHEN UPPER([subject]) = 'B' THEN report ELSE NULL END) report_subjectB,
MIN(CASE WHEN UPPER([subject]) = 'C' THEN report ELSE NULL END) report_subjectC
FROM YourTable
GROUP BY
reporter_id