【发布时间】:2021-09-12 23:36:39
【问题描述】:
我正在尝试编写一个查询,在该查询中我根据其他条件更新计数器。例如:
with table1 as (select *, count from table1)
select box_type,
case when box_type = lag(box_type) over (order by time)
then
count, update table1 set count = count + 1
else
count
end as identifier
这是我正在尝试做的基本要点。我想要一个如下所示的表格:
box_type identifier
small 1
small 1
small 1
medium 2
medium 2
large 3
large 3
small 4
我最初需要在 Postgresql 中执行此操作。解决办法是
select t1.*,
count(*) filter (where box_type is distinct from prev_box_type) over (order by time) as count
from (select t1.*,
lag(box_type) over (order by time) as prev_box_type
from table1 t1
) t1
但我无法让它在 Snowflake 语法中工作。
谢谢!
【问题讨论】:
标签: sql postgresql snowflake-cloud-data-platform snowflake-schema