力扣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;
相关推荐
oradh10 小时前
Oracle数据库扩展区(extent)概述
数据库·oracle·oracle基础·oracle数据库扩展区概述
开源Z11 小时前
LeetCode 238 · 除自身以外数组的乘积:左右两遍扫描,不用除法
算法·leetcode
8Qi811 小时前
LeetCode 5:最长回文子串(Longest Palindromic Substring)—— 题解
算法·leetcode·职场和发展·动态规划
如竟没有火炬21 小时前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
8Qi81 天前
LeetCode 1143 & 718:最长公共子序列 / 最长重复子数组
算法·leetcode·职场和发展·动态规划
你想考研啊1 天前
mysql数据库导出导入
数据库·mysql·oracle
想吃火锅10051 天前
【leetcode】1.两数之和js版
javascript·算法·leetcode
qq21084629531 天前
【数据库】TDengine 清理旧数据
数据库·oracle·tdengine
initialize13061 天前
Postgresql(Oracle兼容) 到Oracle19.9字符语义
数据库·oracle
phltxy1 天前
MCP 从协议到 Spring AI 实战
人工智能·spring·oracle