48. Apples vs Oranges
In a fruit sales business, it is important to track the sales of different fruits like apples and oranges. Understanding the sales difference between these fruits each day can help in inventory management, pricing strategies, and promotional activities.
You are given a table named Sales:
โโโโโโโโโโโโโโโฆโโโโโโโโโโโ โ Column Name โ Type โ โ โโโโโโโโโโโโโโฌโโโโโโโโโโโฃ โ sale_date โ date โ โโโโโโโโโโโโโโโผโโโโโโโโโโโ โ fruit โ enum โ โโโโโโโโโโโโโโโผโโโโโโโโโโโ โ sold_num โ int โ โโโโโโโโโโโโโโโฉโโโโโโโโโโโ
- sale_date: The date of the sale.
- fruit: The type of fruit.
- sold_num: The number of fruits sold.
- Primary Key: (sale_date, fruit).
Write a query to calculate the difference between the number of apples and oranges sold for each day. The result should be sorted by sale_date.
Example 1:
Example:
Input:
Sales Table
โโโโโโโโโโโโโโฆโโโโโโโโโโฆโโโโโโโโโโโ โ sale_date โ fruit โ sold_num โ โ โโโโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโฃ โ 2020-05-01 โ apples โ 10 โ โโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโ โ 2020-05-01 โ oranges โ 8 โ โโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโ โ 2020-05-02 โ apples โ 15 โ โโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโ โ 2020-05-02 โ oranges โ 15 โ โโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโ โ 2020-05-03 โ apples โ 20 โ โโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโ โ 2020-05-03 โ oranges โ 0 โ โโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโ โ 2020-05-04 โ apples โ 15 โ โโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโ โ 2020-05-04 โ oranges โ 16 โ โโโโโโโโโโโโโโฉโโโโโโโโโโฉโโโโโโโโโโโ
Expected Output:
โโโโโโโโโโโโโโฆโโโโโโโ โ sale_date โ diff โ โ โโโโโโโโโโโโโฌโโโโโโโฃ โ 2020-05-01 โ 2 โ โโโโโโโโโโโโโโผโโโโโโโ โ 2020-05-02 โ 0 โ โโโโโโโโโโโโโโผโโโโโโโ โ 2020-05-03 โ 20 โ โโโโโโโโโโโโโโผโโโโโโโ โ 2020-05-04 โ -1 โ โโโโโโโโโโโโโโฉโโโโโโโ
Explanation:
- On 2020-05-01, 10 apples and 8 oranges were sold, so the difference is 10 - 8 = 2.
- On 2020-05-02, 15 apples and 15 oranges were sold, so the difference is 15 - 15 = 0.
- On 2020-05-03, 20 apples and 0 oranges were sold, so the difference is 20 - 0 = 20.
- On 2020-05-04, 15 apples and 16 oranges were sold, so the difference is 15 - 16 = -1.
Still unsure what the problem is asking ?
Letโs go through a few more examples, step by step, to make it clearer.