gpt4 book ai didi

sql - 从一列创建多列

转载 作者:行者123 更新时间:2023-12-02 02:17:04 25 4
gpt4 key购买 nike

我的数据库结构如下:

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 语句从上面的数据中调用它。谁能帮帮我吗?基本上我想根据与其关联的月份将数字列拆分为多个其他列。我正在使用 sql-server 2000

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

最佳答案

这个过程称为旋转。一种方法:

 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

关于sql - 从一列创建多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11724953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com