力扣数据库进阶50--No.1445题解

官方难度中等,体感中等,算是经典启蒙题。

核心是先做行转列新增每日苹果、橙子两列数量,再聚合。
select sale_date,
    sum(case when fruit = 'apples' then sold_num else 0 end) - sum(case when fruit = 'oranges' then sold_num else 0 end) as diff
from Sales
group by sale_date
order by sale_date

这里用max代替sum,同样能处理行转列后出现null或0的结构性问题。

e5d4f548d97fcc4d869fdd0f2cf6c319

其他题解,也有比较简洁的方法,采用负数进行反向计数。

posted @ 2026-08-30 21:24  找回那所有、  阅读(6)  评论(0)    收藏  举报
这里到底了哦~(●'◡'●)