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
上面是我的代码,用于将数据转换为我需要的格式,但结果显示为
。
我缺少的是使结果看起来像
的额外位。
我试图在案件之前和案件中使用max并且没有工作。如果我仅按日期分组,我会丢失大部分数据,这与分组按国家/地区相同。我错过了什么?
答案 0 :(得分:3)
您想要条件聚合,但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;
之外:
package my.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@RestController
public class MyController {
private static final String VALIDATION_REGEX = "^[0-9]+(,[0-9]+)*$";
@RequestMapping(value = "/my/{id}", method = GET)
public myResonseObject getMyParams(@PathVariable("id") String id,
@Valid @Pattern(regexp = VALIDATION_REGEX)
@RequestParam(value = "myparam", required = true) String myParam) {
// Do Stuff!
}
}
答案 1 :(得分:0)
那么可能是子查询?
SELECT date, MAX(france), MAX(germany), MAX(italy), MAX(netherlands), MAX(Nordic), MAX(spain)
FROM
(
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
)
GROUP BY date
我暂时没有使用mySql,我无法测试它。 :(
答案 2 :(得分:0)
嗯,你已经硬编了国名,所以这不是那么糟糕......
SELECT
Date,
Sum(France) as France,
Sum(Germany) as Germany,
Sum(Italy) as Italy,
Sum(Netherlands) as Netherlands,
Sum(Nordic) as Nordic,
Sum(Spain) as Spain,
Sum(Uk) as UK
FROM
(
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
)SUB_Q
GROUP BY Date