0

Suppose we have two columns and we know possible values:

a = [1,2]
b = [1,2,3]

So, the table with all combinations will be:

a, b
----
1, 1
1, 2
1, 3
2, 1
2, 2
2, 3

But what if the table is not full?:

a, b
----
1, 1
--
--
--
2, 2
2, 3

How to find the missing combinations?:

a, b
----
1, 2
1, 3
2, 1

P.S. The table is quite large actually, it contains 1-2 million records.
It contains other values, not only that we have to check, those values should be ignored.
Actually, it is not necessary to get all the missing combinations.
Find at least one is enough.

1
  • 2
    I guess the idea is: select distinct a-values, select distinct b-values, cross join them. SELECT * FROM CROSS_JOIN EXCEPT SELECT EXISTING_COMBINATIONS Commented Sep 7, 2022 at 10:51

1 Answer 1

1

You can use EXCEPT to get that result:

select t1.a, t2.b
from unnest(array[1,2]) as t1(a)
  cross join unnest(array[1,2,3]) as t2(b)
except  
select a, b
from the_table
order by 1,2

The first part generates all possible values, and the EXCEPT will then return those that are in the first query, but not in the second (which is the content of your table)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.