我正在尝试使用 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;
它失败了, where s 与横向子查询的 where 子句中的引用有关。
SQL Error [42601]: ERROR: syntax error at or near "s"
我为横向查询尝试了不同的方法,但我总是得到同样的错误。我不知道我错过了什么。
如果我运行 CTE 表达式并从中选择 s.*,我会得到预期的结果,因此该部分工作正常。
我在 CentOS 上运行 Postgres 11.6。