nomnom3214
nomnom3214

Reputation: 259

Multiple filter for 1 condition in sql

I want to count how many type that have a c and approved status from the table below.

type   status_1  status_2   status_3    status_4     status_5    status_6
1         a      revised      c         approved       null       null
2         a      approved     c         approved       null       null
3         b      rejected     a         revised         c        approved       
4         a      rejected     b         rejected        c        approved_with_comment   

The status column always comes in pairs. So, status_1 paired with status_2, status_3 paired with status_4, status_5 with status_6. I want to create a logic with query that state: number of type that has been c and approved.

The query is supposed to express:
status_1 = c and status_2 = approved
status_3 = c and status_4 = approved
status_5 = c and status_6 = approved

With the table above, the query should result 3 type.

Can somebody help on how I write the query in order to get the desired result?

Thanks in advance.

Upvotes: 0

Views: 35

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269593

You should fix the data model, so you don't have such repeated data. There should be separate rows in a separate table rather than repeated columns.

If I understand correctly, you want:

select count(*)
from t
where ( 'c', 'approved') in ( (status_1, status_2), (status_3, status_4), (status_5, status_6) );

Upvotes: 1

Related Questions