【问题标题】:How to calculate totals for a filter with a window function?如何计算具有窗口函数的过滤器的总数?
【发布时间】:2015-10-23 22:55:32
【问题描述】:

我有 Postgres 9.3 数据库和一个包含许多行的表。我有一个过滤器表达式,想计算sumcount 3 种类型:

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 完成所有三个查询吗? (没有工会等)

我已经阅读了有关窗口函数的文档和其他示例,但这对我来说仍然完全模糊。

附:我知道我可以使用with 子句或嵌套查询,但只是为了学习,我想尝试使用窗口函数/聚合表达式。

【问题讨论】:

    标签: postgresql postgresql-9.3 window-functions


    【解决方案1】:

    示例数据:

    create table a_table (id int, val int);
    insert into a_table values
    (1, 1), (2, 2), (3, 3);
    

    aggregate expressions 的查询:

    select
        sum(val) filter (where id = 1) sum1,
        sum(val) filter (where id != 1) sum2,
        sum(val) sum3
    from a_table;
    

    【讨论】:

    • 我看到你显示聚合表达式的底部,这就是我需要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-09-27
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    相关资源
    最近更新 更多