0

lets say I had the following two columns (brand & color) the values are:

brand   color
ford    blue
ford    pink
ford    yellow
chevy   green
chevy   white
chevy   yellow
jeep    blue
jeep    green

I only care about the brands that sell a pink OR blue car. what type of window function would allow me to only pull back the THREE 'ford' records because ford has a pink / blue car. And the TWO jeep records. (I don't want to see any chevy records). thanks

2 Answers 2

1

You should do a distinct query on the brand column with the color condition. you will ge the list of brands which are viable for querying. then join this result with the normal table based on the brand. and then get the record.

in other words you need a subquery with the distinct brands and the condition which is inner joined to your normal table

something like

select t1.* 
from t1 
inner join (
  select distinct brand
  from t1 
  where color in ('pink', 'blue') 
  ) as t2 on t2.brand = t1.brand
Sign up to request clarification or add additional context in comments.

Comments

0
SELECT *
FROM t1
WHERE brand in (
                 SELECT brand
                 FROM t1
                 WHERE color in ('pink', 'blue') 
               )

Read this query in English: select all records from table where brand equals to all brands with color pink or blue.

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.