如何将一个增加的排名 - 而不是与rank()和dense_rank()相同的值分配给以前遇到的后一组值?

时间:2017-07-28 15:31:04

标签: sql postgresql

date        id          b   bc      x
2017-06-01  a35b3y26f   3   0.19    1
2017-06-02  a35b3y26f   3   0.19    1
2017-06-03  a35b3y26f   3   0.23    2
2017-06-04  a35b3y26f   3   0.12    3
2017-06-05  a35b3y26f   3   0.21    4
2017-06-06  a35b3y26f   3   0.19    5
2017-06-07  a35b3y26f   3   0.28    6
2017-06-08  a35b3y26f   3   0       7
2017-06-09  a35b3y26f   3   0       7
2017-06-10  a35b3y26f   3   0.15    8
2017-06-11  a35b3y26f   3   0.3     9
2017-06-12  a35b3y26f   3   0.17    10
2017-06-13  a35b3y26f   3   0.27    11
2017-06-14  a35b3y26f   3   0.28    12
2017-06-15  a35b3y26f   3   0.18    13
2017-06-16  a35b3y26f   3   0       14
2017-06-17  a35b3y26f   3   0.2     15
2017-06-18  a35b3y26f   3   0       16
2017-06-19  a35b3y26f   3   0.28    17
2017-06-20  a35b3y26f   3   0.25    18
2017-06-21  a35b3y26f   3   0.19    19
2017-06-22  a35b3y26f   3   0.23    20
2017-06-23  a35b3y26f   3   0       21
2017-06-24  a35b3y26f   3   0       21
2017-06-25  a35b3y26f   3   0.13    22

上面,列x表示我希望在结果集中输出的值。

有没有办法使用PostgreSQL提供的现有窗口函数,我可以获得这个结果?

1 个答案:

答案 0 :(得分:1)

一种方法是使用sumlag函数:

SELECT "date", "id", "b", "bc", "x",
        SUM( xxxxx ) OVER (order by "date") As X
FROM (
   SELECT *,
          CASE "bc" 
          WHEN lag( "bc" ) over (order by "date")
          THEN 0 ELSE 1 
          END as xxxxx
   FROM table1
) x

演示:http://sqlfiddle.com/#!17/8dab6/4

|                 date |        id | b |   bc |  x |  x |
|----------------------|-----------|---|------|----|----|
| 2017-06-01T00:00:00Z | a35b3y26f | 3 | 0.19 |  1 |  1 |
| 2017-06-02T00:00:00Z | a35b3y26f | 3 | 0.19 |  1 |  1 |
| 2017-06-03T00:00:00Z | a35b3y26f | 3 | 0.23 |  2 |  2 |
| 2017-06-04T00:00:00Z | a35b3y26f | 3 | 0.12 |  3 |  3 |
| 2017-06-05T00:00:00Z | a35b3y26f | 3 | 0.21 |  4 |  4 |
| 2017-06-06T00:00:00Z | a35b3y26f | 3 | 0.19 |  5 |  5 |
| 2017-06-07T00:00:00Z | a35b3y26f | 3 | 0.28 |  6 |  6 |
| 2017-06-08T00:00:00Z | a35b3y26f | 3 |    0 |  7 |  7 |
| 2017-06-09T00:00:00Z | a35b3y26f | 3 |    0 |  7 |  7 |
| 2017-06-10T00:00:00Z | a35b3y26f | 3 | 0.15 |  8 |  8 |
| 2017-06-11T00:00:00Z | a35b3y26f | 3 |  0.3 |  9 |  9 |
| 2017-06-12T00:00:00Z | a35b3y26f | 3 | 0.17 | 10 | 10 |
| 2017-06-13T00:00:00Z | a35b3y26f | 3 | 0.27 | 11 | 11 |
| 2017-06-14T00:00:00Z | a35b3y26f | 3 | 0.28 | 12 | 12 |
| 2017-06-15T00:00:00Z | a35b3y26f | 3 | 0.18 | 13 | 13 |
| 2017-06-16T00:00:00Z | a35b3y26f | 3 |    0 | 14 | 14 |
| 2017-06-17T00:00:00Z | a35b3y26f | 3 |  0.2 | 15 | 15 |
| 2017-06-18T00:00:00Z | a35b3y26f | 3 |    0 | 16 | 16 |
| 2017-06-19T00:00:00Z | a35b3y26f | 3 | 0.28 | 17 | 17 |
| 2017-06-20T00:00:00Z | a35b3y26f | 3 | 0.25 | 18 | 18 |
| 2017-06-21T00:00:00Z | a35b3y26f | 3 | 0.19 | 19 | 19 |
| 2017-06-22T00:00:00Z | a35b3y26f | 3 | 0.23 | 20 | 20 |
| 2017-06-23T00:00:00Z | a35b3y26f | 3 |    0 | 21 | 21 |
| 2017-06-24T00:00:00Z | a35b3y26f | 3 |    0 | 21 | 21 |
| 2017-06-25T00:00:00Z | a35b3y26f | 3 | 0.13 | 22 | 22 |