【问题标题】:how can I use window function - difficult case in PostgreSQL如何使用窗口函数 - PostgreSQL 中的困难案例
【发布时间】:2017-05-03 17:47:28
【问题描述】:

我有一张如下表:

group    sequence   action          
1        1          promotion_1               
1        2          promotion_1               
1        3          promotion_2               
1        4          act1                      
1        5          act1                     
1        6          act2                      
1        7          promotion_1               
1        8          act1                                  
1        9          act2                     
1       10          promotion_1               
1       11          act1                     
2        1          promotion_2                 
2        2          act1                  

我想创建一个排名,它将显示促销范围。所以我需要有相同的数字进行促销和促销后的每一个动作:

group    sequence   action         parameter   
1        1          promotion_1    1             
1        2          promotion_1    2              
1        3          promotion_2    3              
1        4          act1           3              
1        5          act1           3            
1        6          act2           3            
1        7          promotion_1    4            
1        8          act1           4                        
1        9          act2           4            
1       10          promotion_1    5            
1       11          act1           5           
2        1          promotion_2    1              
2        2          act1           1   

我了解了窗口函数及其可能性(例如 over()),但我只能通过“操作”分组创建排名。

【问题讨论】:

    标签: sql postgresql amazon-redshift postgresql-8.0


    【解决方案1】:
    select T.*,
           sum(case when action like 'promotion%' then 1 else 0 end)
             over(partition by "group" order by sequence rows unbounded preceding) parameter
      from Table T
    

    【讨论】:

    • 我收到错误:带有 ORDER BY 子句的聚合窗口函数需要一个框架子句。你能帮帮我吗?
    • order by sequence之后添加rows unbounded precedingrows between unbounded preceding and current row
    • @alarson008 已更正。感谢 DuduMarkovitz
    【解决方案2】:

    filter(从 PostgeSQL 9.4 开始)

    select  *
           ,count (*) filter (where action like 'promotion%') over 
            (
                partition by  "group" 
                order by      sequence
                rows          unbounded preceding    
            )
    
    from    mytable  T
    

    +-------+----------+-------------+-------+
    | group | sequence |   action    | count |
    +-------+----------+-------------+-------+
    |     1 |        1 | promotion_1 |     1 |
    |     1 |        2 | promotion_1 |     2 |
    |     1 |        3 | promotion_2 |     3 |
    |     1 |        4 | act1        |     3 |
    |     1 |        5 | act1        |     3 |
    |     1 |        6 | act2        |     3 |
    |     1 |        7 | promotion_1 |     4 |
    |     1 |        8 | act1        |     4 |
    |     1 |        9 | act2        |     4 |
    |     1 |       10 | promotion_1 |     5 |
    |     1 |       11 | act1        |     5 |
    |     2 |        1 | promotion_2 |     1 |
    |     2 |        2 | act1        |     1 |
    +-------+----------+-------------+-------+
    

    【讨论】:

    • 我收到错误“错误:在“(”位置:36 或附近出现语法错误。当我从互联网复制带有过滤器的示例代码时:SELECT count() AS unfiltered, count () FILTER (WHERE i
    • 与您的版本无关。您是否有机会使用 Redshift(或者您只是喜欢老式版本)?
    • 嗯...我使用 Redshift(亚马逊红移)
    • 修复了你的标签。
    • 不客气。不需要其他解决方案,迈克的解决方案是经典解决方案,您可以接受。如果您愿意,可以使用 count(case when action like 'promotion%' then 1 end) 代替 sum(...)
    猜你喜欢
    • 2012-12-30
    • 2020-11-13
    • 2018-12-15
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多