QuestionQ98
VerifiedAnalyzing QueriesA data analyst has a sales table containing the columns product_id, sale_date, and sale_amount. The analyst needs to calculate total sales for each product on each day, returning exactly one row for every product/day.
They initially use the following SQL query:
SELECT product_id, sale_date, sale_amount,
SUM(sale_amount) OVER (PARTITION BY product_id ORDER BY sale_date) AS daily_total
FROM sales
ORDER BY product_id, sale_date;
However, it returns multiple rows for the same product and date rather than a single aggregated row for each product/date combination.
Which corrected SQL query returns exactly one row per product per day, with that day’s total sales?
Community Discussion