The problem requires calculating the daily difference between the number of apples and oranges sold. The solution leverages SQL's grouping and aggregation capabilities.
The core idea is to:
sale_date
: This ensures we calculate the difference for each day independently.sold_num
for apples and subtract the sold_num
for oranges. This is efficiently done using conditional aggregation (e.g., MySQL's IF
function or similar functions in other SQL dialects).sale_date
in ascending order.SELECT
sale_date,
SUM(IF(fruit = 'apples', sold_num, -sold_num)) AS diff
FROM Sales
GROUP BY sale_date
ORDER BY sale_date;
Explanation:
SELECT sale_date, SUM(...) AS diff
: This selects the sale_date
and calculates the sum, aliasing it as diff
.SUM(IF(fruit = 'apples', sold_num, -sold_num))
: This is the crucial part. The IF
function checks the fruit
column.
fruit
is 'apples', it adds sold_num
to the sum (positive contribution).fruit
is 'oranges', it subtracts sold_num
from the sum (negative contribution).FROM Sales
: This specifies the table to query.GROUP BY sale_date
: This groups the rows by sale_date
, so the SUM
function operates on each day's sales separately.ORDER BY sale_date
: This sorts the final result by sale_date
in ascending order.GROUP BY
operation. In general, the time complexity of GROUP BY
is O(N log N) or O(N) depending on the specific database implementation and the presence of indexes. N is the number of rows in the Sales
table.GROUP BY
operation. In the worst case, if there are unique dates for each row, the space complexity would be O(N). However, in practice, this is often much smaller because there will likely be multiple sales entries for the same date. The space used for the final result set is proportional to the number of unique dates, which is typically much less than N.The core logic can be adapted to other SQL dialects. Here's an example using PostgreSQL:
SELECT
sale_date,
SUM(CASE WHEN fruit = 'apples' THEN sold_num ELSE -sold_num END) AS diff
FROM Sales
GROUP BY sale_date
ORDER BY sale_date;
The CASE
statement in PostgreSQL serves the same purpose as the IF
function in MySQL. Similar conditional expressions exist in other database systems like SQL Server, Oracle, etc. The overall approach remains consistent across different SQL dialects.