力扣1069-产品销售分析II

销售表:Sales

复制代码
+-------------+-------+
| Column Name | Type  |
+-------------+-------+
| sale_id     | int   |
| product_id  | int   |
| year        | int   |
| quantity    | int   |
| price       | int   |
+-------------+-------+
sale_id 是这个表的主键(具有唯一值的列)。
product_id 是 Product 表的外键(reference 列)。
该表的每一行显示产品product_id在某一年的销售情况。
请注意价格是每单位的。

产品表:Product

复制代码
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| product_id   | int     |
| product_name | varchar |
+--------------+---------+
product_id 是这个表的主键(具有唯一值的列)。
该表的每一行表示每种产品的产品名称。

编写解决方案,统计每个产品的销售总量。

返回结果表 无顺序要求 。

结果格式如下例子所示。

示例 1:

复制代码
输入:
Sales 表:
+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+ 
| 1       | 100        | 2008 | 10       | 5000  |
| 2       | 100        | 2009 | 12       | 5000  |
| 7       | 200        | 2011 | 15       | 9000  |
+---------+------------+------+----------+-------+
Product 表:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100        | Nokia        |
| 200        | Apple        |
| 300        | Samsung      |
+------------+--------------+
输出:
+--------------+----------------+
| product_id   | total_quantity |
+--------------+----------------+
| 100          | 22             |
| 200          | 15             |
+--------------+----------------+

思路:

1、对product_id进行分组;

2、sum函数对数量quantitiy求和;

3、注意输出名称;

代码:

复制代码
select 
product_id,
sum(quantity) as total_quantity  
from sales 
group by product_id;
相关推荐
Rain的Java大神之路7 小时前
LeetCode 45 跳跃游戏 II:大厂面试官最爱追问的贪心题,我把骨头给你拆干净了
java·后端·leetcode
不喝水就会渴1 天前
「不想上班」实战系列:密码卡证保险箱 —— 主密钥存入 Asset,密文落库 RDB【鸿蒙心迹】
数据库·oracle
让我们一起加油好吗2 天前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
Rain的Java大神之路2 天前
LeetCode 42「接雨水」:面试官百出不厌的题,一文讲透从暴力到双指针 O(1)
java·后端·leetcode
飞花漫卷,情随岁迁2 天前
网络数据包转发与内核机制:小白概念科普
linux·运维·网络·数据库·mysql·oracle·智能路由器
隔窗听雨眠3 天前
长周期Oracle RAC重启决策与滚动执行手册
oracle·rac·数据库运维
八月瓜科技3 天前
专精特新企业如何搭建专利资产中台?三步落地法
大数据·数据库·人工智能·科技·深度学习·机器学习·oracle
Navigator_Z4 天前
LeetCode //C - 1320. Minimum Distance to Type a Word Using Two Fingers
c语言·算法·leetcode
wy_hhxx4 天前
[Leetcode] 滑动窗口
算法·leetcode·滑动窗口
AIgorithmGEEK5 天前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash