gpt4 book ai didi

mysql - 选择给定年份的金额

转载 作者:行者123 更新时间:2023-11-30 23:32:05 25 4
gpt4 key购买 nike

我有一张表格,其中包含国家、年份和每年的金额。我正在尝试编写一个查询,该查询将对给定年份的总金额求和并将其作为列提供给我。

例如,输出如下所示:

Country   1991     1992     1993
France 25 12 38

我试过这样的查询,但无济于事:

SELECT 
country as "Recipient",
SUM(usd_amount) AS "1991" WHEN project_year = 1991,
SUM(usd_amount) AS "1992" WHEN project_year = 1992,
SUM(usd_amount) AS "1993" WHEN project_year = 1993

FROM tb1

GROUP BY project_year, country
order by country, project_year

有什么建议吗?谢谢!克里斯

最佳答案

试一试:

SELECT 
country as "Recipient",
SUM(if(project_year = 1991, usd_amount, 0)) AS "1991",
SUM(if(project_year = 1992, usd_amount, 0)) AS "1992",
SUM(if(project_year = 1993, usd_amount, 0)) AS "1993",
FROM tb1
GROUP BY country
ORDER BY country

PS:按项目年份排序没有意义,因为您已经在汇总了。

关于mysql - 选择给定年份的金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10069631/

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