我怎样才能得到每列的总和?
How can I get sum total of each column?
创建临时文件TABLE
CREATE TEMP TABLE total(
gid SERIAL,
zoom smallint NOT NULL,
point integer NOT NULL,
size integer NOT NULL
);
插入数据
INSERT INTO total(zoom, point, size) VALUES(9,51,21);
INSERT INTO total(zoom, point, size) VALUES(9,75,45);
INSERT INTO total(zoom, point, size) VALUES(9,74,34);
INSERT INTO total(zoom, point, size) VALUES(10,75,4);
INSERT INTO total(zoom, point, size) VALUES(10,72,63);
INSERT INTO total(zoom, point, size) VALUES(10,85,22);
计算点数,根据 ZOOM 增加尺寸
SELECT zoom,
count(*) AS point,
SUM(size) AS size
FROM total
GROUP BY zoom
ORDER BY zoom;
结果:
zoom | point | size
------+-------+------
9 | 3 | 100
10 | 3 | 89
(2 rows)
问题
我怎样才能 return 每列的总数?
想要的结果:
zoom | point | size
------+-------+------
9 | 3 | 100
10 | 3 | 89
------+-------+------
Total | 6 | 189
模拟汇总的方法是简单地 运行 执行汇总的第二个查询。但是,列中的所有值必须具有相同的数据类型。由于要显示标签 'Total'
,因此还需要将数字 zoom
从基本查询转换为文本:
但是因为你想按实际缩放值排序,你还需要在结果中保留整数值。
sort_order
是必要的,以确保并集第一部分的行实际保留 "at the top"
select zoom, point, size
FROM (
SELECT zoom::text as zoom,
zoom as zoom_value,
count(*) AS point,
SUM(size) AS size,
1 as sort_order
FROM total
GROUP BY zoom
UNION ALL
SELECT 'Total',
null,
count(*) AS point,
SUM(size) AS size,
2 as sort_order
FROM total
) t
order by sort_order, zoom_value;
这个returns:
zoom | point | size
------+-------+-----
9 | 3 | 100
10 | 3 | 89
Total | 6 | 189
使用最新的 Postgres 版本,您可以执行以下操作:
SELECT case when grouping(zoom) = 1 then 'Total' else zoom::text end,
count(*) AS point,
SUM(size) AS size
FROM total
GROUP BY rollup (zoom)
order by zoom;
创建临时文件TABLE
CREATE TEMP TABLE total(
gid SERIAL,
zoom smallint NOT NULL,
point integer NOT NULL,
size integer NOT NULL
);
插入数据
INSERT INTO total(zoom, point, size) VALUES(9,51,21);
INSERT INTO total(zoom, point, size) VALUES(9,75,45);
INSERT INTO total(zoom, point, size) VALUES(9,74,34);
INSERT INTO total(zoom, point, size) VALUES(10,75,4);
INSERT INTO total(zoom, point, size) VALUES(10,72,63);
INSERT INTO total(zoom, point, size) VALUES(10,85,22);
计算点数,根据 ZOOM 增加尺寸
SELECT zoom,
count(*) AS point,
SUM(size) AS size
FROM total
GROUP BY zoom
ORDER BY zoom;
结果:
zoom | point | size
------+-------+------
9 | 3 | 100
10 | 3 | 89
(2 rows)
问题
我怎样才能 return 每列的总数?
想要的结果:
zoom | point | size
------+-------+------
9 | 3 | 100
10 | 3 | 89
------+-------+------
Total | 6 | 189
模拟汇总的方法是简单地 运行 执行汇总的第二个查询。但是,列中的所有值必须具有相同的数据类型。由于要显示标签 'Total'
,因此还需要将数字 zoom
从基本查询转换为文本:
但是因为你想按实际缩放值排序,你还需要在结果中保留整数值。
sort_order
是必要的,以确保并集第一部分的行实际保留 "at the top"
select zoom, point, size
FROM (
SELECT zoom::text as zoom,
zoom as zoom_value,
count(*) AS point,
SUM(size) AS size,
1 as sort_order
FROM total
GROUP BY zoom
UNION ALL
SELECT 'Total',
null,
count(*) AS point,
SUM(size) AS size,
2 as sort_order
FROM total
) t
order by sort_order, zoom_value;
这个returns:
zoom | point | size
------+-------+-----
9 | 3 | 100
10 | 3 | 89
Total | 6 | 189
使用最新的 Postgres 版本,您可以执行以下操作:
SELECT case when grouping(zoom) = 1 then 'Total' else zoom::text end,
count(*) AS point,
SUM(size) AS size
FROM total
GROUP BY rollup (zoom)
order by zoom;