Skip to content
Advertisement

SQL Count all null values as distinct [closed]

I need to write a query that tells me how many(COUNT) unique customers made a purchase of over $200. My table has a customer_id, purchase_id, and total. purchase_id is unique for every row. Customer_id can repeat, and also have null values.

A “unique customer is considered a unique customer_id or a null customer_id. So in the list:

1
2
1
null
null
null

There would be 5 unique customers.

Advertisement

Answer

SELECT (
(SELECT COUNT DISTINCT customer_id FROM TABLE where total > 200 and customer_id IS NOT NULL) +
(SELECT COUNT customer_id FROM TABLE where total > 200 and customer_id IS NULL)
)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement