[英]Aggregate in pivot in SQL Server
I try this 我尝试这个
declare @t as table (yea int, rating varchar(100))
insert into @t
values (2012, 'US'), (2013, 'S'), (2014, 'G'),
(2015, 'E'), (2016, 'E')
--select * from @T
select
[2012], [2013], [2014], [2015], [2016]
from
(select rating
from @T) p
pivot
(max (rating)
for rating in ([2012], [2013], [2014], [2015], [2016])) as pb
and I got this result: 我得到了这个结果:
2012 2013 2014 2015 2016
--------------------------------------
NULL NULL NULL NULL NULL
but I would really like this result instead: 但我真的很想要这个结果:
2012 2013 2014 2015 2016
-------------------------------------
US S G E E
Is this null data because of max(rating)
? 这是因为max(rating)
是否为空数据吗? If so - how do I get the desired data? 如果是这样-我如何获得所需的数据?
You were missing yea (not sure if yea was a typo so I left it as is) 您想念是 (不确定是不是错字,所以我照原样保留了)
declare @t as table (yea int, rating varchar(100))
insert into @t values (2012,'US')
insert into @t values (2013,'S')
insert into @t values (2014,'G')
insert into @t values (2015,'E')
insert into @t values (2016,'E')
--select * from @T
select [2012],[2013],[2014],[2015],[2016]
from
(select yea,rating
from @T
) p
pivot
(max (rating) for yea in ([2012],[2013],[2014],[2015],[2016])) as pb
Returns 退货
2012 2013 2014 2015 2016
US S G E E
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.