如何使用 window 函数计算过滤器的总数?
How to calculate totals for a filter with a window function?
我有 Postgres 9.3 数据库和一个有很多行的 table。我有一个过滤器表达式,想计算 3 种类型的 sum
或 count
:
1) 表达式为真
2)表达式为假
3) 所有行
计算第一个的例子:
select count(*) from osm_polygon where building in ('dormitory', 'офис', 'office',
'school', 'kindergarten', 'residential', 'public', 'yes', 'house',
'apartments', 'roof', 'detached', 'civic', 'shop', 'apartments;yes', 'hotel'));
是否可以用 window function 进行所有三个查询? (没有工会等)
我已经阅读了有关 window 函数的文档和其他示例,这对我来说仍然是完全模糊的。
P.S。我知道我可以使用 with
子句或嵌套查询,但只是为了学习,我想尝试使用 window functions/aggregate 表达式。
示例数据:
create table a_table (id int, val int);
insert into a_table values
(1, 1), (2, 2), (3, 3);
select
sum(val) filter (where id = 1) sum1,
sum(val) filter (where id != 1) sum2,
sum(val) sum3
from a_table;
我有 Postgres 9.3 数据库和一个有很多行的 table。我有一个过滤器表达式,想计算 3 种类型的 sum
或 count
:
1) 表达式为真 2)表达式为假 3) 所有行
计算第一个的例子:
select count(*) from osm_polygon where building in ('dormitory', 'офис', 'office',
'school', 'kindergarten', 'residential', 'public', 'yes', 'house',
'apartments', 'roof', 'detached', 'civic', 'shop', 'apartments;yes', 'hotel'));
是否可以用 window function 进行所有三个查询? (没有工会等)
我已经阅读了有关 window 函数的文档和其他示例,这对我来说仍然是完全模糊的。
P.S。我知道我可以使用 with
子句或嵌套查询,但只是为了学习,我想尝试使用 window functions/aggregate 表达式。
示例数据:
create table a_table (id int, val int);
insert into a_table values
(1, 1), (2, 2), (3, 3);
select
sum(val) filter (where id = 1) sum1,
sum(val) filter (where id != 1) sum2,
sum(val) sum3
from a_table;