sql - PostgreSQL - rank over rows listed in blocks of 0 and 1 -


i have table looks like:

id     code      date1         date2         block -------------------------------------------------- 20     1234      2017-07-01    2017-07-31    1 15     1234      2017-06-01    2017-06-30    1 13     1234      2017-05-01    2017-05-31    0 11     1234      2017-03-01    2017-03-31    0 9      1234      2017-02-01    2017-02-28    1 8      1234      2017-01-01    2017-01-31    0 7      1234      2016-11-01    2016-11-31    0 6      1234      2016-10-01    2016-10-31    1 2      1234      2016-09-01    2016-09-31    1 

i need rank rows according blocks of 0's , 1's, like:

id     code      date1         date2         block     desired_rank ------------------------------------------------------------------- 20     1234      2017-07-01    2017-07-31    1         1 15     1234      2017-06-01    2017-06-30    1         1 13     1234      2017-05-01    2017-05-31    0         2 11     1234      2017-03-01    2017-03-31    0         2 9      1234      2017-02-01    2017-02-28    1         3 8      1234      2017-01-01    2017-01-31    0         4 7      1234      2016-11-01    2016-11-31    0         4 6      1234      2016-10-01    2016-10-31    1         5 2      1234      2016-09-01    2016-09-31    1         5 

i've tried use rank() , dense_rank(), result end is:

id     code      date1         date2         block     dense_rank() ------------------------------------------------------------------- 20     1234      2017-07-01    2017-07-31    1         1 15     1234      2017-06-01    2017-06-30    1         2 13     1234      2017-05-01    2017-05-31    0         1 11     1234      2017-03-01    2017-03-31    0         2 9      1234      2017-02-01    2017-02-28    1         3 8      1234      2017-01-01    2017-01-31    0         3 7      1234      2016-11-01    2016-11-31    0         4 6      1234      2016-10-01    2016-10-31    1         4 2      1234      2016-09-01    2016-09-31    1         5 

in last table, rank doesn't care rows, takes 1's , 0's unit , sets ascending count starting @ first 1 , 0. query goes this:

create temp table data (id integer,code text, date1 date, date2 date, block integer);  insert data values (20,'1234', '2017-07-01','2017-07-31',1), (15,'1234', '2017-06-01','2017-06-30',1), (13,'1234', '2017-05-01','2017-05-31',0), (11,'1234', '2017-03-01','2017-03-31',0), (9, '1234', '2017-02-01','2017-02-28',1), (8, '1234', '2017-01-01','2017-01-31',0), (7, '1234', '2016-11-01','2016-11-30',0), (6, '1234', '2016-10-01','2016-10-31',1), (2, '1234', '2016-09-01','2016-09-30',1);  select *,dense_rank() on (partition code,block order date2 desc) data  order date2 desc; 

by way, database in postgresql.

i hope there's workaround... :)

edit: note blocks of 0's , 1's aren't equal.

there's no way result using single window function:

select *,    sum(flag) -- sum 0/1 create "rank"    on (partition code          order date2 desc)  (    select *,       case          when lag(block) -- check if 1st row of new block               on (partition code                     order date2 desc) = block           0           else 1        end flag    data  ) dt 

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 -

Add new key value to json node in java -