I have pairs of values that I'd like to group with common ID.
Sample data FIDDLE
create table sample_data(value_a integer, value_b integer);
insert into sample_data values
(100,200),
(400,500),
(400,600),
(800,900),
(800,1500),
(1000,800);
So far I tried this one, which works only when common value is value_a
select value_a as value, dense_rank() over(order by value_a) as group_id
from sample_data
UNION
select value_b as value, dense_rank() over(order by value_a) as group_id
from sample_data
order by 2,1
First two groups are fine, but I want last 3 rows from the table to be grouped together like this:
100, 1
200, 1
400, 2
500, 2
600, 2
800, 3
900, 3
1000, 3
1500, 3