Home > Back-end >  SQL query to find products which have different values for a particular id which should be the same
SQL query to find products which have different values for a particular id which should be the same

Time:11-28

I need a SQL query which will fetch me the list of products which has different values for a same id and product is of table A and Id and values are of table B and both the tables can be joined by column name prod_id

enter image description here

Output I want:

  • List item
product
abc

So in output I want only the product abc because it has different values for their respective id and I don’t need xyz because it has same values for their respective id

I tried but I’m not getting what I want as mentioned above

CodePudding user response:

select distinct product
from your_table
group by product, id
having count(distinct values) > 1

CodePudding user response:

this query seems to do the trick

SELECT distinct product from mytemp group by product, id, `values` having count(1) = 1
  • Related