我用 -
创建了一个表width + padding + border = actual visible/rendered width of an element's box,
height + padding + border = actual visible/rendered height of an element's box.
在其中插入了一些值并尝试使用此查询
CREATE TABLE sales(year INT, country VARCHAR(20), product VARCHAR(32), profit INT);
获得的结果 -
SELECT GROUP_CONCAT(country) as country, year, SUM(profit) AS profit
FROM sales
GROUP BY year ASC WITH ROLLUP;
如图所示 - 最后一行连接所有行值。有没有办法我可以用NULL替换最后一行的值,就像年份一样?
我需要country | year | profit
----------------------------------------------|-------|----------
India,Australia | 2014 | 50
----------------------------------------------|-------|----------
New Zealand | 2015 | 20
----------------------------------------------|-------|----------
United States | 2016 | 150
----------------------------------------------|-------|----------
India,Australia,New Zealand,United States | NULL | 220
使用GROUP_CONCAT
而不能country
groupby
获取country
。只想用空的val替换最后一行值。
答案 0 :(得分:1)
你应该避免汇总
select country, year, profit
from (
SELECT '1' seq, GROUP_CONCAT(country) as country, year, SUM(profit) AS profit
FROM sales
GROUP BY seq, year ;
union
select '2', null, null, SUM(profit)
SELECT
FROM sales
) t
order bt t.seq, t.country
答案 1 :(得分:1)
GROUP BY WITH ROLLUP使用起来有点棘手,有时你会得到奇怪的结果。
我建议您使用UNION和第二个查询来获得所需的结果。
SELECT GROUP_CONCAT(country) as country, year, SUM(profit) AS profit
FROM sales
GROUP BY year ASC
UNION ALL
SELECT NULL, NULL, SUM(profit) AS profit FROM sales