How to select value that matches all values in a list?

Tomer Shalhon

I am trying to get two separate row values, for 2 conditions on the same column.

for example with this data:

id       status
----------------
1        0
1        2
1        3
2        2
2        0

I want to select all the rows where the status = 0, and status = 2.

So for example the output should be:

id       status
----------------
1        0
1        2
2        0
2        2

Thank you!

Gordon Linoff

One method is:

where status in (0, 2)

But I suspect you want both values for the id. In that case, one method uses exists:

select t.*
from t
where status in (0, 2) and
      exists (select 1
              from t t2
              where t2.id = t.id and
                    t2.status in (0, 2) and
                    t2.status <> t.status
             );

If you just want the ids, then aggregation is easy:

select id
from t
where status in (0, 2)
group by id
having count(*) = 2;

This can be incorporated in a query to get the original rows using in, exists, or join. Or window functions:

select t.*
from (select t.*,
             count(*) filter (where status in (0, 2)) over (partition by id) as cnt
      from t
     ) t
where cnt = 2;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

select group of rows that matches all its associated items to a list

How to check if all elements of a list matches a condition?

How to select rows where a combination of columns matches a list of values?

Select where all of list in 1 table matches some in another

How to set all values of a Python list of lists to a specific value?

How to write a list of lower triangular matrices where the sum of all matches values are equal to one

How to pythonically select a random index from a 2D list such that the corresponding element matches a value?

Only select one row with matches list column value

Compare all row values agains list and count matches

how to get the list of all values in a row if particular value found in column

Selecting nested values with a list inside a dict, when a value matches

MySQL: Select records where joined table matches ALL values

Select column value that matches a combination of other columns values on the same table

How to select a sub XML that matches a value in PLSQL?

Select rows where all criteria matches from a key value table

SQL How to select data that has all values above a value

PL SQL query to find a list of identifiers that matches all values in a list

How do I scale values all the values in a list where the largest value is 1 and the smallest value is 0

How to get all values from a list of dict that matches key prefix?

How to Select By Value or Select All

How to update all bigquery tables which contains column and who values matches specific value?

laravel, how to select with all values

select all row values as a list

How to select row with maximum value if all values are NA in R

Return objects containing all values if 1 value matches a string

How to find all lower and upper values for a given value in a periodic list

How to find all lower and upper values for a given value in a list

Google Sheets, Select all WHERE cell matches value in a column

How to get all checked value as list on click select all checkbox?