admin

从一列创建多列

sql

我的数据库的结构如下:

Number | Month | Country
------------------------
63     | June  | Ireland
48     | June  | England
55     | June  | Spain
66     | May   | Ireland
33     | May   | England
53     | May   | Spain
44     | April | Ireland
44     | April | England
44     | April | Spain

我想使用一条SQL语句从上面的数据中调用它。谁能帮帮我。基本上,我想根据与之关联的月份将number列拆分为其他多个列。我正在使用sql-server
2000

Country | June | May | April
---------------------------
Ireland | 63   | 66  |  44
England | 48   | 33  |  44
Spain   | 55   | 53  |  44

阅读 209

收藏
2021-07-01

共1个答案

admin

该过程称为枢转。一种方法是:

 select  Country
 ,       sum(case when Month = 'June' then Number end) as June
 ,       sum(case when Month = 'May' then Number end) as May
 ,       sum(case when Month = 'April' then Number end) as April
 from    YourTable
 group by
         Country
2021-07-01