If I have a table that logs how many products were sold each day - and does not put in a log if 0 items were sold - how would I go about identifying all of my products had a day of 0 sales?
CafeTable sample
Date Item QuantitySold
Jan 1 coffee 4
Jan 1 tea 1
Jan 2 tea 3
Jan 3 coffee 2
Jan 3 tea 4
Desired output would be something like this. So far I can get to a version that lists each day and quantity sold, but can't figure out if there's a way to also "tag" the product.
Item TotalSold SoldDaily
coffee 6 FALSE
tea 8 TRUE
This is my rough starting point - the JOIN is to try to force all dates to show, but it's still skipping the row for coffee on Jan 2. And from there, I can't figure out how I would potentially then perform the next step - I've tried a few CASE WHENs but not coming together.
with dates as (select distinct date from cafetable)
select date,
product,
sum(quantitysold) as quant_sold,
min(quantitysold) as min_sold
from dates c
left join cafetable d on d.date=c.date
group by 1,2
I don't think I'm even articulating this well. I work with this table a dozen times a day and I feel like this query has fried my brain.


