在雪花中,如何在下表中参考CTE
我们在雪花中有以下工作查询:
with
latest_track_metrics as (
select * from track_metrics
where "week_id" = (select max("week_id") from track_metrics)
)
select * from latest_track_metrics
为了稍微清理此代码,我们很想从track_metrics 中选择select max(“ week_id'')到其自己的行中,带有变量名称,因此:
with
max_weekid as (select max("week_id") from track_metrics),
latest_track_metrics as (
select * from track_metrics
where "week_id" = max_weekid // error on this line, not recognizing max_weekid
)
select * from latest_track_metrics
但是后一个查询返回错误错误:无效标识符'max_weekid'(第5行)
。我们试图将max_weekid
与括号,报价,背景等包装。所有这些都没有运气。是否可以以这种方式调用CTE?
We have the following working query in snowflake:
with
latest_track_metrics as (
select * from track_metrics
where "week_id" = (select max("week_id") from track_metrics)
)
select * from latest_track_metrics
in an effort to clean this code up a bit, we'd love to refactor the select max("week_id") from track_metrics
into its own row with variable name, as such:
with
max_weekid as (select max("week_id") from track_metrics),
latest_track_metrics as (
select * from track_metrics
where "week_id" = max_weekid // error on this line, not recognizing max_weekid
)
select * from latest_track_metrics
However the latter query returns the error Error: invalid identifier 'MAX_WEEKID' (line 5)
. We've tried to wrap the max_weekid
with parenthesis, quotes, backticks, etc. all with no luck. Is it possible to call a CTE in this manner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用合格和窗口的最大值:
Using QUALIFY and windowed MAX:
将
max_weekid
作为子查询或表。而不是
,Week_ID =
,您应该将其与Track_metrics一起使用JOIN
子句。也许这样的东西:
Treat
max_weekid
as a subquery or a table.Instead of
WHERE week_id =
, you should be joining it together with track_metrics with aJOIN
clause.Perhaps something like this:
错误是您将“ Week_ID”列与CTE“ max_weekid”匹配。 SQL应该是
The error is that you are matching a column "week_id" to a CTE "max_weekid". SQL should be