简体   繁体   中英

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:

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

Is there any way to do this pivot in 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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