Distribute values

All we need is an easy explanation of the problem, so here it is.

I would like to distribute for each unique value in col1 an unique value of col2 in an ascendant order.

DATA

This will be always n^n, where n is the number of values assignable to an unique value of col1

 -------------
| col1 | col2 |
|------|------|
|    a |     1|
|    a |     2|
|    a |     3|
|    b |     1|
|    b |     2|
|    b |     3|
|    c |     1|
|    c |     2|
|    c |     3|
---------------

On the first loop, I want to distribute ‘a’ to the first available value which is ‘1’. As ‘a’ has a value, {a: 2} and {a: 3} are removed. And as ‘1’ is assigned, {b: 1} and {c: 1} are removed.

So the expected result at the end

 -------------
| col1 | col2 |
|------|------|
|    a |     1|
|    b |     2|
|    c |     3|
---------------

My tests

The first query could be SELECT DISTINCT ON (col1) * FROM ... ORDER BY col1 ASC, but the result is:

 -------------
| col1 | col2 |
|------|------|
|    a |     1|
|    b |     1|
|    c |     1|
---------------

So I would like to execute something like that:

SELECT DISTINCT ON (col1) AND DISTINCT ON (col2) *
FROM ... 
ORDER BY col1, col2 ASC

With the first DISTINCT execution:

 -------------
| col1 | col2 |
|------|------|
|    a |     1|
|    b |     1|
|    b |     2|
|    b |     3|
|    c |     1|
|    c |     2|
|    c |     3|
---------------

And the second one:

 -------------
| col1 | col2 |
|------|------|
|    a |     1|
|    b |     2|
|    b |     3|
|    c |     2|
|    c |     3|
---------------

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You sample data makes it seem like this could be the solution:

SELECT col1, col2
FROM  (
   SELECT *
        , dense_rank() OVER (ORDER BY col1) AS col1_rnk
        , row_number() OVER (PARTITION BY col1 ORDER BY col2) AS col2_rn
   FROM   tbl
   ) sub
WHERE  col1_rnk = col2_rn
ORDER  BY col1, col2;  -- optional

db<>fiddle here

This is really a guess, though, as your problem is barely defined.
Among other things this assumes that every distinct value in col1 is combined with the same set of values in col2.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply