简体   繁体   English

在SQL中旋转简单表

[英]Pivoting a simple table in SQL

I have a simple two-column table, which I want to pivot the rows into columns, so that this table: 我有一个简单的两列表,我想将行透视成列,以便该表:

List      Count   
----      -----
Bugs        3   
Changes     5  

Can look like this table: 可以如下表所示:

Bugs    Changes
----     -----
 3         5   

I have attempted the pivot using an ETL Tool of Uncollapsing Columns, but keep ending up with this: 我曾尝试使用ETL工具的“不折叠列”进行数据透视,但最终还是这样:

Bugs    Changes
----     -----
 3        null 
null       5

Is there any way to do this pivot in SQL? 有什么办法可以在SQL中做到这一点?

You can do aggregation like that : 您可以像这样进行聚合:

select sum(case when List = 'Bugs' then Count else 0 end) as Bugs,
       sum(case when List = 'Changes' then Count else 0 end) as Changes
from table t
where List in ('Bugs', 'Changes');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM