EDIT: I came to the following query - no window functions:
WITH RECURSIVE tree AS ( SELECT k, v, ''::text as next_k, 0 as next_v, 0 AS level FROM window_test UNION ALL SELECT ck, cv, tk, tv + level, t.level + 1 FROM tree t JOIN window_test c ON ck = tk AND cv + 1 = tv), partitions AS ( SELECT tk, tv, t.next_k, coalesce(nullif(t.next_v, 0), tv) AS next_v, t.level FROM tree t WHERE NOT EXISTS (SELECT 1 FROM tree WHERE next_k = tk AND next_v = tv)) SELECT min(k) AS k, v AS min_v, max(next_v) AS max_v FROM partitions p GROUP BY v ORDER BY 2;
I have provided 2 work requests now, hope one of them puts you down.
SQL Fiddle for this option.
Another way to achieve this is to use a support sequence.
Create a support sequence:
CREATE SEQUENCE wt_rank START WITH 1;
Inquiry:
WITH source AS ( SELECT k, v, coalesce(lag(k) OVER (ORDER BY v), k) AS prev_k FROM window_test CROSS JOIN (SELECT setval('wt_rank', 1)) AS ri), ranking AS ( SELECT k, v, prev_k, CASE WHEN k = prev_k THEN currval('wt_rank') ELSE nextval('wt_rank') END AS rank FROM source) SELECT rk, min(sv) AS min_v, max(sv) AS max_v FROM ranking r JOIN source s ON rv = sv GROUP BY r.rank, rk ORDER BY 2;
source share