简体   繁体   English

如何在 Snowflake sql 中通过分区和排序来计算不同的值?

[英]How to count distinct value with partition by and order by in Snowflake sql?

My data is as follows:我的数据如下:

| user | eventorder| postal|
|:---- |:---------:| -----:|
| A    | 1         | 60616 |
| A    | 2         | 10000 |
| A    | 3         | 60616 |
| B    | 1         | 20000 |
| B    | 2         | 30000 |
| B    | 3         | 40000 |
| B    | 4         | 30000 |
| B    | 5         | 20000 |

The problem I need to solve: how many distinct stops until each event order that user has travelled?我需要解决的问题:在用户旅行的每个事件订单之前有多少不同的停靠点?

The ideal result should be as follows:理想的结果应该如下:

| user | eventorder| postal| travelledStop|
|:---- |:---------:| -----:| ------------:|
| A    | 1         | 60616 |  1    |
| A    | 2         | 10000 |  2    |
| A    | 3         | 60616 |  2    |
| B    | 1         | 20000 |  1    |
| B    | 2         | 30000 |  2    |
| B    | 3         | 40000 |  3    |
| B    | 4         | 30000 |  3    |
| B    | 5         | 20000 |  3    |

Take A as an example, when event order is 1, it only travelled 60616 - 1 stop.以 A 为例,当事件顺序为 1 时,它仅行进 60616 - 1 站。 When event order is 2, it has travelled 60616 and 10000 - 2 stops.当事件顺序为 2 时,它已行驶 60616 和 10000 - 2 站。 When event order is 3, the distinct stops this user has travelled are 60616 and 10000. - 2 stops.当事件顺序为 3 时,此用户经过的不同站点是 60616 和 10000。 - 2 个站点。

I am not allowed to use count distinct with partition by order by.我不允许将 count distinct 与 partition by order by 一起使用。 I want to do something like count(distinct(postal)) over (partition by user order by eventorder), but it is not allowed.我想做一些类似 count(distinct(postal)) 的事情(按用户顺序按 eventorder 分区),但这是不允许的。

Does anyone know how to solve this?有谁知道如何解决这个问题? Thanks a lot!非常感谢!

I used the sample data you provided (a subset of just A, but this should scale out).我使用了您提供的示例数据(只是 A 的一个子集,但这应该可以扩展)。 The goal here is to essentially generate an array for each row that accumulates all the postals for the previous events.这里的目标本质上是为每一行生成一个数组,该数组累积了先前事件的所有邮政。

with _temp as (
select 'A' as usr, 1 as EventOrder, '60616' as Postal
UNION ALL
select 'A' as usr, 2 as EventOrder, '10000' as Postal
UNION ALL
select 'A' as usr, 3 as EventOrder, '60616' as Postal
),
_intermediate as (
select usr
    , eventorder
    , postal
    , array_slice(
          array_agg(postal)
            within group (order by eventorder)
            OVER (Partition by usr)
           , 0, eventorder) as full_array
from _temp
group by usr, eventorder, postal
)
select usr, eventorder, postal, count(distinct f.value)
from _intermediate i, lateral flatten(input => i.full_array) f
group by usr, eventorder, postal

Perhaps the simplest method is to use a subquery and count the "1"s:也许最简单的方法是使用子查询并计算“1”:

select t.*,
       sum(case when seqnum = 1 then 1 else 0 end) over (partition by usr order by eventorder) as num_postals
from (select t.*,
             row_number() over (partition by usr, postal order by eventorder) as seqnum
      from t
     ) t

I like @Daniel Zagales answer but here is a work-around by using dense_rank and sum我喜欢@Daniel Zagales 的回答,但这里是使用dense_ranksum的解决方法

with temp as (
select 'A' as usr, 1 as EventOrder, '60616' as Postal
UNION ALL
select 'A' as usr, 2 as EventOrder, '10000' as Postal
UNION ALL
select 'A' as usr, 3 as EventOrder, '60616' as Postal  
UNION ALL
select 'B' as usr, 1 as EventOrder, '20000' as Postal  
UNION ALL
select 'B' as usr, 2 as EventOrder, '30000' as Postal  
UNION ALL
select 'B' as usr, 3 as EventOrder, '40000' as Postal 
UNION ALL
select 'B' as usr, 4 as EventOrder, '30000' as Postal  
UNION ALL
select 'B' as usr, 5 as EventOrder, '20000' as Postal 
),
temp2 as(
select temp.* ,dense_rank()over(partition by usr,Postal order by EventOrder) rks
from temp 
)
select usr,eventorder,postal,sum(case when rks = 1 then 1 else 0 END)over(partition by usr order by EventOrder) travelledStop
from temp2 
order by usr,EventOrder 

basically use dense_rank to get first appear stop than sum up.基本上使用dense_rank来获得第一次出现的停止而不是总结。

db<>fiddle db<>小提琴

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

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