【问题标题】:How to get the total count between two values in postgreSQL如何获取postgreSQL中两个值之间的总数
【发布时间】:2021-08-10 23:06:12
【问题描述】:

我在单个列 (completed_order, email, viewed) 中有 3 个活动,我想计算在每个 completed_order 活动之间发生了多少 email 活动并将它们保存在不同的列中。 我写了这个查询:

SELECT  activity_id, ts, customer, activity ,
        case when activity = 'completed_order'      
        then count(*)filter (where activity = 'email' ) over (partition by customer order by ts )
        else null end as Aggregate_in_between
    FROM public.activity_stream as az1  where customer = 'Lehmanns Marktstand' order by ts ;

我通过上述查询得到以下结果。

activity_id ts customer activity agg_in_btw
11089 "1996-08-12 00:00:00+05" "Lehmanns Marktstand" "completed_order" 0
10279 "1996-08-13 00:00:00+05" "Lehmanns Marktstand" "completed_order" 0
11077 "1996-08-14 00:00:00+05" "Lehmanns Marktstand" "email"
11092 "1996-08-17 00:00:00+05" "Lehmanns Marktstand" "viewed_page"
11088 "1996-08-18 00:00:00+05" "Lehmanns Marktstand" "viewed_page"
10284 "1996-08-19 00:00:00+05" "Lehmanns Marktstand" "completed_order" 1
11078 "1996-08-20 00:00:00+05" "Lehmanns Marktstand" "email"
11079 "1996-08-21 00:00:00+05" "Lehmanns Marktstand" "email"
11080 "1996-10-21 00:00:00+05" "Lehmanns Marktstand" "email"
10343 "1996-10-31 00:00:00+05" "Lehmanns Marktstand" "completed_order" 4
11090 "1996-11-01 00:00:00+05" "Lehmanns Marktstand" "viewed_page"
11091 "1996-11-02 00:00:00+05" "Lehmanns Marktstand" "email"
10497 "1997-04-04 00:00:00+05" "Lehmanns Marktstand" "completed_order" 5
10522 "1997-04-30 00:00:00+05" "Lehmanns Marktstand" "completed_order" 5

我想要的结果应该是这样的

activity_id ts customer activity agg_in_btw
11089 "1996-08-12 00:00:00+05" "Lehmanns Marktstand" "completed_order" 0
10279 "1996-08-13 00:00:00+05" "Lehmanns Marktstand" "completed_order" 1
11077 "1996-08-14 00:00:00+05" "Lehmanns Marktstand" "email"
11092 "1996-08-17 00:00:00+05" "Lehmanns Marktstand" "viewed_page"
11088 "1996-08-18 00:00:00+05" "Lehmanns Marktstand" "viewed_page"
10284 "1996-08-19 00:00:00+05" "Lehmanns Marktstand" "completed_order" 3
11078 "1996-08-20 00:00:00+05" "Lehmanns Marktstand" "email"
11079 "1996-08-21 00:00:00+05" "Lehmanns Marktstand" "email"
11080 "1996-10-21 00:00:00+05" "Lehmanns Marktstand" "email"
10343 "1996-10-31 00:00:00+05" "Lehmanns Marktstand" "completed_order" 1
11090 "1996-11-01 00:00:00+05" "Lehmanns Marktstand" "viewed_page"
11091 "1996-11-02 00:00:00+05" "Lehmanns Marktstand" "email"
10497 "1997-04-04 00:00:00+05" "Lehmanns Marktstand" "completed_order" 0
10522 "1997-04-30 00:00:00+05" "Lehmanns Marktstand" "completed_order" 0

【问题讨论】:

  • 我已经添加了答案。希望这会有所帮助

标签: sql postgresql count window-functions


【解决方案1】:

试试这个方法:

select  activity_id, ts, customer, activity ,
case when activity = 'completed_order' then
sum(case when activity='email' then 1 else 0 end) over (partition by customer,grp1)
else
null
end "Aggregate_in_between"
from (
select *, sum(grp) over (partition by customer order by ts) "grp1" from (
select *, case when activity='completed_order' then 1 else 0 end "grp" from activity_stream order by ts  
) t ) q

在这里,您必须创建一个包含每个 completed_order 出现的组,然后他们计算每个组中的 email

您可以根据自己的方便添加 where 子句

DEMO

【讨论】:

    【解决方案2】:

    这是一个分组问题。根据“已完成”的累积计数分配一个组。然后在每组内计数:

    select a.*,
           count(*) filter (where activity = 'email') over (partition by co_grp)
    from (select a.*,
                 count(*) filter (where activity = 'completed_order') over (partition by customer order by ts) as co_grp
          from public.activity_stream a
          where customer = 'Lehmanns Marktstand'
         ) a
    order by ts ; 
    

    在您的示例数据中,您似乎只希望在已完成的订单行上使用此功能,因此请使用 case 表达式:

    select a.*,
           (case when activity = 'completed_order'
                 then count(*) filter (where activity = 'email') over (partition by co_grp)
            end) as agg_in_btw
    from (select a.*,
                 count(*) filter (where activity = 'completed_order') over (partition by customer order by ts) as co_grp
          from public.activity_stream a
          where customer = 'Lehmanns Marktstand'
         ) a
    order by ts ; 
    

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      相关资源
      最近更新 更多