Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to show all 3 urls are null in following table in postgresql/sql

I want to show records where url is null and typeurl of type1 & type2 also null.
Output should be productname : Oil

ProductName     Url    Type     TypeUrl
Shampoo         null   Type1    null
Shampoo         null   Type2    www.domain.com
Conditioner     null   Type1    null
Conditioner     null   Type2    www.domain.com
Oil             null   Type1    null
Oil             null   Type2    null

postgresql query I want

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We can use a simple aggregation here:

SELECT ProductName
FROM yourTable
GROUP BY ProductName
HAVING COUNT(Url) = 0 AND
       COUNT(TypeUrl) FILTER (WHERE Type IN ('Type1', 'Type2')) = 0;

For versions of Postgres which do not support FILTER, use this version:

SELECT ProductName
FROM yourTable
GROUP BY ProductName
HAVING COUNT(Url) = 0 AND
       COUNT(CASE WHEN Type IN ('Type1', 'Type2') THEN TypeUrl END) = 0;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading