下一级的LAG()/ LEAD()(Postgresql)

时间:2018-03-28 12:40:09

标签: sql postgresql

有没有办法强制PostgreSQL LAG()LEAD()函数使用的值不是来自前导行而是来自同一分区的下一个等级

---------------------------------------------------
| client_id | order_id | product_id | year | rank |
---------------------------------------------------
|     1     |    1     |   111345   | 1995 |  1   |
|     1     |    1     |   912346   | 1995 |  1   |
|     1     |    1     |   212346   | 1995 |  1   |
|     1     |    2     |   233368   | 1998 |  4   |
|     1     |    2     |   133368   | 1998 |  4   |
|     1     |    3     |   412341   | 2005 |  6   |
|     2     |    55    |   312344   | 1995 |  1   |
|     2     |    57    |   812343   | 1999 |  2   |
---------------------------------------------------

预期结果将是:

---------------------------------------------------------------------------
| client_id | order_id | product_id | year | rank | prev_year | next_year |
---------------------------------------------------------------------------
|     1     |    1     |   111345   | 1995 |  1   |    null   |   1998    |
|     1     |    1     |   912346   | 1995 |  1   |    null   |   1998    |
|     1     |    1     |   212346   | 1995 |  1   |    null   |   1998    |
|     1     |    2     |   233368   | 1998 |  4   |    1995   |   2005    |
|     1     |    2     |   133368   | 1998 |  4   |    1995   |   2005    |
|     1     |    3     |   412341   | 2005 |  6   |    1998   |   null    |
|     2     |    55    |   312344   | 1995 |  1   |    null   |   1999    |
|     2     |    57    |   812343   | 1999 |  2   |    1995   |   null    |    
---------------------------------------------------------------------------

如果year在给定排名中具有不同的值,则prev_yearnext_year可以是这些值中的任何一个。例如:

---------------------------------------------------------------------------
| client_id | order_id | product_id | year | rank | prev_year | next_year |
---------------------------------------------------------------------------
|     1     |    1     |   111345   | 1994 |  1   |    null   |   1998    |
|     1     |    1     |   912346   | 1995 |  1   |    null   |   1998    |
|     1     |    1     |   212346   | 1996 |  1   |    null   |   1998    |
|     1     |    2     |   233368   | 1998 |  4   |    ????   |   null    |

????可以等于1994年,1995年或1996年

1 个答案:

答案 0 :(得分:2)

您应该对数据集使用lag()lead()函数,每对(client_id, rank)减少为一行:

select 
    client_id, order_id, product_id, 
    t.year, rank, prev_year, next_year
from my_table t
join (
    select distinct on (client_id, rank)
        client_id, rank, year, 
        lag(year) over w as prev_year, 
        lead(year) over w as next_year
    from my_table
    window w as (partition by client_id order by rank)
    order by 1, 2, 3 desc
    ) s using (client_id, rank)
order by client_id, rank

 client_id | order_id | product_id | year | rank | prev_year | next_year 
-----------+----------+------------+------+------+-----------+-----------
         1 |        1 |     212346 | 1995 |    1 |           |      1998
         1 |        1 |     912346 | 1995 |    1 |           |      1998
         1 |        1 |     111345 | 1995 |    1 |           |      1998
         1 |        2 |     133368 | 1998 |    4 |      1995 |      2005
         1 |        2 |     233368 | 1998 |    4 |      1995 |      2005
         1 |        3 |     412341 | 2005 |    6 |      1998 |          
         2 |       55 |     312344 | 1995 |    1 |           |      1999
         2 |       57 |     812343 | 1999 |    2 |      1995 |          
(8 rows)