gpt4 book ai didi

mysql - 我的 Select Case 有问题,输出不工作

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:13 24 4
gpt4 key购买 nike

SELECT date,
(CASE country WHEN 'France' THEN (sum(duration) / sum(scheduled))*100 ELSE 0 END) AS france,
(CASE country WHEN 'Germany' THEN (sum(duration) / sum(scheduled))*100 ELSE 0 END) AS germany,
(CASE country WHEN 'Italy' THEN (sum(duration) / sum(scheduled))*100 ELSE 0 END) AS italy,
(CASE country WHEN 'Netherlands' THEN (sum(duration) / sum(scheduled))*100 ELSE 0 END) AS netherlands,
(CASE country WHEN 'Nordic' THEN (sum(duration) / sum(scheduled))*100 ELSE 0 END) AS nordic,
(CASE country WHEN 'Spain' THEN (sum(duration) / sum(scheduled))*100 ELSE 0 END) AS spain,
(CASE country WHEN 'UK' THEN (sum(duration) / sum(scheduled))*100 ELSE 0 END) AS uk
FROM report_adherence_daily
WHERE date>= '2016-07-04'
AND date<= '2016-07-10'
GROUP BY date,
country

上面是我将数据转换为我需要的格式的代码,但结果显示为

Result of the query .

我缺少的是使结果看起来像的额外位

Required Result .

我曾尝试在案例之前和案例中使用 max,但没有奏效。如果我只按日期分组,我会丢失大部分数据,这与按国家/地区分组相同。我错过了什么?

最佳答案

你想要条件聚合,但 case 需要进入 sum() 内部,而不是外部:

SELECT date,
(100 * sum(case when country = 'France' then duration end) /
sum(case when country = 'France' then scheduled end)
) as france,
. . .
FROM report_adherence_daily
WHERE date >= '2016-07-04' AND date <= '2016-07-10'
GROUP BY date
ORDER BY date;

关于mysql - 我的 Select Case 有问题,输出不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38614401/

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