How to equiry data using sql when facing condition?

Home / Uncategorized / How to equiry data using sql when facing condition?

Question:
type cost
A 10
A 11
A 12
B 10
B 10
I have this small sample table. I want to select data where the cost of the same type is different.So the expected outcome should be:type cost
A 10
A 11
A 12
The cost for A is different so I need to select these "A" out. So what is the "select" sentence?


Answer:
You can use EXISTS to look for another row with same type but other cost:select t1.type, t1.cost
from tablename t1
where exists (select * from tablename t2
where t2.type = t1.type
and t2.cost <> t1.cost)
Or have a sub-query that returns type values having different costs, and join with that result:select t1.type, t1.cost
from tablename t1
join (select type
from tablename
group by type
having max(cost) <> min(cost)) t2
on t1.type = t2.type
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *