Skip to content
Advertisement

Check if all values in a column falls into a specific group

I have the following table:

and I want to check ALL values of the column ‘Token’ and return a flag string to designate what condition is met:

  • when all records are in group (A, B) –> return 'Condition1',
    • examples:

  • when all records are in group (A, B, C) –> return 'Condition2'
    • examples:

so I am basically looking for a general SELECT statement that looks like:

or any other form of SQL that provide the desired results.

Advertisement

Answer

You can use LISTAGG() aggregate function to get all the distinct values of Token as a comma separated list and check the result in a CASE expression:

See the demo.

Note that in recent versions of Oracle you can include DISTINCT inside LISTAGG():

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement