sql - How can one assign a rank that increases--rather than the same value as rank() and dense_rank() does--to later group of values previously encountered? -


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 

above, column x represents values wish have outputted in result-set.

is there way using existing windowing functions provided postgresql can obtain outcome?

one way use sum , lag functions:

select "date", "id", "b", "bc", "x",         sum( xxxxx ) on (order "date") x (    select *,           case "bc"            when lag( "bc" ) on (order "date")           0 else 1            end xxxxx    table1 ) x 

demo: 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 | 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -