CREATE TEMPORARY 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)
问题
如何返回每列的总数?
通缉结果:
zoom | point | size
------+-------+------
9 | 3 | 100
10 | 3 | 89
------+-------+------
Total | 6 | 189
答案 0 :(得分:3)
模拟汇总的方法是简单地运行第二个执行汇总的查询。但是,列中的所有值必须具有相同的数据类型。如果要显示标签result.isSuccess()
,则需要将基本查询中的数字'Total'
转换为文本:
但是,由于您希望按实际缩放值排序,您还需要在结果中保留整数值。
zoom
是必要的,以确保联盟第一部分的行实际上保持“在顶部”
sort_order
返回:
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;
使用最新的Postgres版本,您可以执行以下操作:
zoom | point | size
------+-------+-----
9 | 3 | 100
10 | 3 | 89
Total | 6 | 189
答案 1 :(得分:-1)
SELECT
zoom,
SUM(point) AS point,
SUM(size) AS size
FROM
(
SELECT zoom,
count(*) AS point,
SUM(size) AS size
FROM total
GROUP BY zoom
) x
ORDER BY zoom