Leetcode Problem 1384. Total Sales Amount by Year

1384. Total Sales Amount by Year

Leetcode Solutions

Calculating Total Sales Amount Per Year Using Date Ranges

  1. Create a Common Table Expression (CTE) or a derived table that generates a series of dates for each year within the range of period_start and period_end from the Sales table.
  2. Join the Product table with the CTE or derived table on product_id.
  3. Filter the dates to include only those within the period_start and period_end range for each product.
  4. Group the results by product_id, product_name, and year, and calculate the sum of average_daily_sales for each group.
  5. Order the final result by product_id and year.

erDiagram
    Product {
        int product_id PK
        varchar product_name
    }

    Sales {
        int product_id PK
        date period_start
        date period_end
        int average_daily_sales
    }

    Product ||--o{ Sales : contains

Using Yearly Subqueries to Calculate Total Sales

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...