1

I have a working query as

   SELECT p.id, 
          p.name AS ProductName, 
          count(DISTINcT s.salesid) as Sales, 
          Count(DISTINCT l.linkid) as Links
     FROM products p
LEFT JOIN sales s ON p.id=s.productid
LEFT JOIN links l ON p.id=l.productid 
 GROUP BY p.id

Now, I need only those records where either sales is not equal to 0 or links is not equal to 0 or both are not equal to 0

How can I achieve this?

0

1 Answer 1

3

Add a HAVING clause

SELECT p.id, p.name AS ProductName, 
count(DISTINcT s.salesid) as Sales, Count(DISTINCT l.linkid) as Links
FROM products p
LEFT JOIN sales s ON p.id=s.productid
LEFT JOIN links l ON p.id=l.productid 
GROUP BY p.id
HAVING Sales > 0 OR Links > 0
Sign up to request clarification or add additional context in comments.

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.