简体   繁体   English

为什么子查询在 Snowflake 中不起作用?

[英]Why is the subquery not working in Snowflake?

I am trying to run this query in Snowflake, but I keep getting an error.我正在尝试在 Snowflake 中运行此查询,但我一直收到错误消息。

select x.*, (select z.status from TBLA z where z.number_id=x.number_id and z.country=x.country and z.datetime=x.datetime) status
from
(
    select a.number_id, a.country, max(datetime) as datetime
    from TBLA a
    group by a.number_id, a.country
) x

This is the error I am getting:这是我得到的错误:

SQL compilation error: Unsupported subquery type cannot be evaluated

Does anyone know how to fix this?有谁知道如何解决这一问题?

To get the status for the latest datetime per number_id/country windowed function could be used:要获取每个 number_id/country 窗口函数的最新日期时间的状态,可以使用:

SELECT a.*, 
  (ARRAY_AGG(a.status) WITHIN GROUP(ORDER BY a.datetime DESC) 
                       OVER(PARTITION BY a.number_id, a.country))[0] AS latest_status
FROM TBLA a;

Looks like you are trying to get the latest status by number_id and country.看起来您正在尝试按 number_id 和国家/地区获取最新状态。 A simple query to do that in Snowflake using window function row_number() is使用窗口函数 row_number() 在 Snowflake 中执行此操作的简单查询是

select * from TBLA
qualify row_number() over (partition by number_id, country order by datetime desc) = 1;

Only scalar subqueries are allowed in the SELECT. SELECT 中只允许使用标量子查询。 Your subquery is not inherently scalar.您的子查询本质上不是标量。

https://docs.snowflake.com/en/user-guide/querying-subqueries.html https://docs.snowflake.com/en/user-guide/querying-subqueries.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM