【问题标题】:Multiple filter (where ...) conditions PostgreSQL多个过滤器(where ...)条件 PostgreSQL
【发布时间】:2023-04-10 11:57:02
【问题描述】:

我正在尝试使用多个过滤器运行以下查询。我不断收到错误消息。如果我单独运行过滤器,则查询运行良好。多个过滤器(where...)不起作用吗?

select count(distinct a.user) as total
    ,count(distinct a.user) filter (where d.date >= current_date - '1 day'::interval) as 1_day
    ,count(distinct a.user) filter (where d.date >= current_date - '3 days'::interval) as 3_day
    ,count(distinct a.user) filter (where d.date >= current_date - '1 week'::interval) as 1_week
    ,count(distinct a.user) filter (where d.date >= current_date - '1 month'::interval) as 1_month
    from ppl d
    join
                (select distinct t.user from tbl t
                join date dd
                    on t.date::date between dd.month_start and dd.month_end
                where dd.date = current_date - '14 days'::INTERVAL
                ) as a
    on d.user = a.user 

我收到此错误:

[Err] ERROR:  syntax error at or near "1"
LINE 2: ....lastedit_date >= current_date - '1 day'::interval) as 1_day
                                                                  ^

【问题讨论】:

  • 我怀疑(虽然我懒得仔细检查)您的字段别名(1_day、3_day、...)不能以数字开头,除非它们被引用。

标签: sql postgresql filter where where-clause


【解决方案1】:

来自docs

SQL 标识符和关键字必须以字母(a-z,还包括带有变音符号和非拉丁字母的字母)或下划线 (_) 开头

这意味着1_day 不是有效的标识符,您不能将其用作不带双引号的列名。

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 2019-08-25
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2023-01-20
    • 1970-01-01
    相关资源
    最近更新 更多