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

Query for checking if every cell is null in a column by id

Below is an example table, I am trying to figure out a way to write a query to put a Y in every cell under READY if all of the STOCK cells are empty for each ID row. So a Y would only show up in the READY columns for ID 1 since OS is in one of the columns for each other ID.

ID STOCK READY
1
1
1
2 OS
2
2
3 OS
3

My first thought is to use a case statement but that puts a Y in every column that has blank STOCK instead of checking if all of the STOCK for each ID is empty. Any ideas how I could accomplish this?

SELECT ID,
STOCK,
CASE WHEN STOCK = '' THEN 'Y'
ELSE '' END AS [READY]
FROM TABLE

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 :

You can use window functions:

select id, stock,
    case when count(stock) over(partition by id) = 0 then 'Y' end as ready
from mytable

The window count takes in account non-null values of stock in rows sharing the same id.

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