30 Days of Distinct Artifacts:Count Unique Artifacts Sold by Category in the Last 30 Days
Intermediate
Measure the unique items sold per category over the last month.
The inventory of cosmic artifacts at Xorthax's trading post is vast. Help him determine how many distinct artifacts (product ids) have been sold in the last 30 days for each category. Use a CTE to filter the data, and then apply a window function.
Write Your Query
Answer:
with recentsales as
(select distinct od.productid,
p.category
from orderdetails od
join products p on od.productid = p.productid
join orders o on od.orderid = o.orderid
where o.orderdate::timestamp >= current_date - interval '30 days' )
select distinct category,
count(productid) over (partition by category) as distinctartifactssold
from recentsales;
Explanation:
This mirrors inventory and sales analysis where a merchant tracks distinct items sold per category within a recent time frame.
You’ll practice filtering recent data, using CTEs for clarity, and performing category-level aggregation with analytical techniques.
This problem is labeled as Intermediate. It assumes you’ve written a few window function queries before, but you don’t need to be an expert. Use the hints and explanations if you get stuck — they’re there to help you think through the logic.
Yes. Every problem comes with optional hints you can reveal one at a time, plus a fully worked step-by-step solution. You decide how much help you want while practicing.
All problems on PracticeWindowFunctions.com are completely free and can be solved without creating an account. Right now there are over 80 practice problems, with new ones added regularly.