问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

LeetCode 1715 苹果和橘子的个数(SQL解决方案)

创作时间:
作者:
@小白创作中心

LeetCode 1715 苹果和橘子的个数(SQL解决方案)

引用
CSDN
1.
https://m.blog.csdn.net/weixin_51696882/article/details/141633882

需求

表: Boxes

Column Name
Type
box_id
int
chest_id
int
apple_count
int
orange_count
int

box_id 是该表的主键。
chest_id 是 chests 表的外键。
该表包含大箱子 (box) 中包含的苹果和橘子的个数。每个大箱子中可能包含一个小盒子 (chest) ,小盒子中也包含若干苹果和橘子。

表: Chests

Column Name
Type
chest_id
int
apple_count
int
orange_count
int

chest_id 是该表的主键。
该表包含小盒子的信息,以及小盒子中包含的苹果和橘子的个数。

编写 SQL 语句,查询每个大箱子中苹果和橘子的个数。如果大箱子中包含小盒子,还应当包含小盒子中苹果和橘子的个数。

以任意顺序返回结果表。

查询结果的格式如下示例所示。

示例 1:

输入:

Boxes 表:

box_id
chest_id
apple_count
orange_count
2
null
6
15
18
14
4
15
19
3
8
4
12
2
19
20
20
6
12
9
8
6
9
9
3
14
16
7

Chests 表:

chest_id
apple_count
orange_count
6
5
6
14
20
10
2
8
8
3
19
4
16
19
19

输出:

apple_count
orange_count
151
123

解释:

大箱子 2 中有 6 个苹果和 15 个橘子。
大箱子 18 中有 4 + 20 (在小盒子中) = 24 个苹果和 15 + 10 (在小盒子中) = 25 个橘子。
大箱子 19 中有 8 + 19 (在小盒子中) = 27 个苹果和 4 + 4 (在小盒子中) = 8 个橘子。
大箱子 12 中有 19 + 8 (在小盒子中) = 27 个苹果和 20 + 8 (在小盒子中) = 28 个橘子。
大箱子 20 中有 12 + 5 (在小盒子中) = 17 个苹果和 9 + 6 (在小盒子中) = 15 个橘子。
大箱子 8 中有 9 + 5 (在小盒子中) = 14 个苹果和 9 + 6 (在小盒子中) = 15 个橘子。
大箱子 3 中有 16 + 20 (在小盒子中) = 36 个苹果和 7 + 10 (在小盒子中) = 17 个橘子。
苹果的总个数 = 6 + 24 + 27 + 27 + 17 + 14 + 36 = 151
橘子的总个数 = 15 + 25 + 8 + 28 + 15 + 15 + 17 = 123

输入

输出

1.查询出每个大箱子中的苹果和橘子数量

select box_id,
       coalesce(b.apple_count,0)+coalesce(c.apple_count,0) as apple_count,
       coalesce(b.orange_count,0)+coalesce(c.orange_count,0) as orange_count
from boxes b
left join chests c
on b.chest_id=c.chest_id;

2.计算苹果和橘子的总数量

with t1 as (
select box_id,
       coalesce(b.apple_count,0)+coalesce(c.apple_count,0) as apple_count,
       coalesce(b.orange_count,0)+coalesce(c.orange_count,0) as orange_count
from boxes b
left join chests c
on b.chest_id=c.chest_id
)
select sum(apple_count) as apple_count,sum(orange_count) as orange_count
from t1;
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号