【问题标题】:Why is my postgres lateral subquery failing?为什么我的 postgres 横向子查询失败?
【发布时间】:2020-05-14 14:56:41
【问题描述】:

我正在尝试使用 postgres 对 LATERAL 子查询的支持运行以下查询:

with s as
    (
        select
            tagValues ->> 'mode' as metric
            , array_agg(id) as ids
        from
            metric_v3.v_series
        where
            name = 'node_cpu'
        group by 1
    )
select
    t.starttime
    , s.metric
    , t.max
from
    s, lateral (
        select
            d.starttime
            , max(d.max) as max
        from
            metric_v3.gaugedata d
        where
            d.starttime >= '2020-01-17T00:00Z' AND  d.starttime < '2020-01-24T00:00Z'
            and d.seriesid in s.ids
        group by 1
    ) t
order by 1,2;

它失败了,其中 s 与横向子查询的 where 子句中的引用相关。

SQL Error [42601]: ERROR: syntax error at or near "s"

我为横向查询尝试了不同的方法,但我总是得到同样的错误。我不知道我错过了什么。

如果我运行 CTE 表达式并从中选择 s.*,我会得到预期的结果,因此该部分工作正常。

我在 CentOS 上运行 Postgres 11.6。

【问题讨论】:

    标签: postgresql lateral-join


    【解决方案1】:

    您不能将IN 与数组一起使用。您需要使用ANY 运算符:

    and d.seriesid = any(s.ids)
    

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多