【发布时间】:2021-12-20 10:52:42
【问题描述】:
我有一个在 Snowflake 中不起作用的子查询。我收到此错误:
SQL compilation error: Unsupported subquery type cannot be evaluated
当我在此查询中运行子查询时,它不起作用。但是,我需要包含此子查询,以便考虑将在 case 语句中使用的“状态”列。
有人可以帮忙吗?
create table TBL_A (
number_id int, country varchar(50), status varchar(50), datetime date
);
insert into TBL_A values
(121144, 'USA', 'CLAIMED', '2021-10-10'),
(121144, 'USA', 'BOUGHT', '2021-10-11'),
(121144, 'USA', 'RETURNED', '2021-10-12'),
(121144, 'AU', 'CLAIMED', '2021-09-10'),
(121144, 'AU', 'BOUGHT', '2021-09-11');
create table TBL_B (
number_id int, country varchar, status varchar,
claimed_date date, bought_date date, returned_date date
);
我需要运行的查询:
insert into TBL_B (number_id, country, status, claimed_date, bought_date, returned_date)
select
w.number_id, w.country, w.status
, case w.status when 'CLAIMED' then w.datetime else '2999-12-31' end claimed_date
, case w.status when 'BOUGHT' then w.datetime else '2999-12-31' end bought_date
, case w.status when 'RETURNED' then w.datetime else '2999-12-31' end returned_date
from
(
select x.*, (select z.status from TBL_A 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 TBL_A a
group by a.number_id, a.country
) x
【问题讨论】:
-
我不认为 Snowflake 在这样的选择中支持相关子查询。
-
看来 this 可以通过简单的连接重写
标签: sql snowflake-cloud-data-platform snowflake-schema