Search code examples
sqlverticarowfilter

SQL: Select rows that have specfic values, independent from the column


I have the following tabel in a vertica DB:

+-----+-------+-------+-------+
| tid | Item1 | Item2 | Item3 |
+-----+-------+-------+-------+
|   1 | A     | B     | C     |
|   2 | B     | D     | A     |
|   3 | C     | D     | A     |
|   4 | D     | B     | A     |
+-----+-------+-------+-------+

I want to find all rows that have the Items A and B, resulting in a table:

+-----+-------+-------+-------+
| tid | Item1 | Item2 | Item3 |
+-----+-------+-------+-------+
|   1 | A     | B     | C     |
|   2 | B     | D     | A     |
|   4 | D     | B     | A     |
+-----+-------+-------+-------+

The order of how A and B occurs is random. Also this is an abstract example, I might want to look for three of four items or even just one item. Is there a simple way checking each row if it contains certain items independant of the order?


Solution

  • select      *
    
    from        mytable
    
    where       'A' in (Item1,Item2,Item3)
            and 'B' in (Item1,Item2,Item3)