You are given a single dataset for a beverage company and must decide whether Q2 2024 truly declined. Write one standard SQL query that produces a comparison table with overall and same-store metrics, guarding against grain mistakes and misleading averages.
Schema:
Sample data (minimal, illustrative): transactions +---------------+------------+----------+------------+-------+---------+-----------+ | transaction_id| sale_date | store_id | product_id | units | revenue | is_return | +---------------+------------+----------+------------+-------+---------+-----------+ | 1 | 2024-03-30 | 101 | 11 | 10 | 50.00 | false | | 2 | 2024-04-02 | 101 | 11 | 8 | 44.00 | false | | 3 | 2024-04-15 | 102 | 12 | 5 | 40.00 | false | | 4 | 2024-05-01 | 103 | 11 | -1 | -5.50 | true | | 5 | 2024-06-20 | 101 | 12 | 7 | 63.00 | false | | 6 | 2023-06-10 | 101 | 11 | 9 | 45.00 | false | +---------------+------------+----------+------------+-------+---------+-----------+
stores +---------+--------+-------------+-------------+ | store_id| region | opened_date | closed_date | +---------+--------+-------------+-------------+ | 101 | West | 2020-01-01 | NULL | | 102 | East | 2021-05-10 | NULL | | 103 | North | 2024-05-01 | 2024-07-15 | +---------+--------+-------------+-------------+
products +------------+----------+ | product_id | category | +------------+----------+ | 11 | Soda | | 12 | Juice | +------------+----------+
Requirements: