Skip to content
Advertisement

How to write SQL query for filtering the row which has two values for a column?

Consider the table as below –

Table A

Parent_Id Child_Id is_active
P1 C1 Y
P1 C2 N
P2 C3 N

Need the SQL query to identify the parent_id which has all child_id with ‘N’. In the above table the output should be

Parent_Id

P2

Advertisement

Answer

SELECT Parent_Id
FROM tableA
GROUP BY Parent_Id
HAVING !SUM(is_active = 'Y')

or

HAVING MAX(is_active) = 'N'
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement