0

I am sure this could be very silly question, but I was not able to find answer from the internet.

I am trying to select rows with this conditon.

Select all rows if Side has both 'Right' and 'Left'.

enter image description here

In this case, it will select both row of Id 3 and 4 (yellow highlighted ones).

I know bottom query is not going to work.

select * from "Table" where ("Side" = 'Right') or ("Side" = 'Left')

How do I go about doing this?

2 Answers 2

2

You need a join. If your table name is processes

select 
 p.*
from processes p

inner join processes p2
on p.pid = p2.pid 
and p.id <> p2.id
and p.side <> p2.side 
Sign up to request clarification or add additional context in comments.

Comments

0

You may filter it using a WHERE & HAVING clause in a GROUP BY

SELECT *
  FROM t
    WHERE pid IN (
       SELECT pid
           FROM t
          WHERE side IN (
           'Right',
           'Left' )
     GROUP BY pid
     HAVING COUNT(DISTINCT side) = 2 )

Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.