【发布时间】:2020-06-09 21:57:13
【问题描述】:
正在从 SQL Server 迁移到 Snowflake。卡在下面的查询中。在雪花中找不到任何等效的东西。 对于外部查询中的每一行,我们进入内部查询以获取比外部查询中的 datekey 小的顶部 datekey。
select a.datekey , a.userid, a.transactionid
,cast( cast(b.datekey as varchar(8)) as date) priorone
from tableA a
outer apply (select top 1 b.datekey
from tableA b where a.userid = b.userid
and b.transactionid < a.transactionid and b.datekey < a.datekey
order by b.transactionid desc) as b
尝试以下建议的答案:
create or replace table tableA
(datekey date , userid int ,transactionid int)
insert into tableA
values('2020-06-01',1,101),('2020-06-02',1,102),('2020-06-02',1,103),('2020-06-01',2,104),('2020-06-02',2,105)
select
a.datekey,
a.userid,
a.transactionid
,(
select b.datekey
from tableA b
where
a.userid = b.userid
and b.transactionid < a.transactionid
and b.datekey < a.datekey
order by b.transactionid desc
limit 1
) priorone
from tableA a
【问题讨论】:
标签: sql date sql-order-by snowflake-cloud-data-platform sql-limit