当前位置:网站首页>1484. 按日期分组销售产品
1484. 按日期分组销售产品
2022-08-05 02:32:00 【只是六号z】
1484. 按日期分组销售产品
前言
表 Activities:
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| sell_date | date |
| product | varchar |
+-------------+---------+
此表没有主键,它可能包含重复项。
此表的每一行都包含产品名称和在市场上销售的日期。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/group-sold-products-by-the-date
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
编写一个 SQL 查询来查找每个日期、销售的不同产品的数量及其名称。
每个日期的销售产品名称应按词典序排列。
返回按 sell_date 排序的结果表。
查询结果格式如下例所示。
示例 1:
输入:
Activities 表:
+------------+-------------+
| sell_date | product |
+------------+-------------+
| 2020-05-30 | Headphone |
| 2020-06-01 | Pencil |
| 2020-06-02 | Mask |
| 2020-05-30 | Basketball |
| 2020-06-01 | Bible |
| 2020-06-02 | Mask |
| 2020-05-30 | T-Shirt |
+------------+-------------+
输出:
+------------+----------+------------------------------+
| sell_date | num_sold | products |
+------------+----------+------------------------------+
| 2020-05-30 | 3 | Basketball,Headphone,T-shirt |
| 2020-06-01 | 2 | Bible,Pencil |
| 2020-06-02 | 1 | Mask |
+------------+----------+------------------------------+
解释:
对于2020-05-30,出售的物品是 (Headphone, Basketball, T-shirt),按词典序排列,并用逗号 ',' 分隔。
对于2020-06-01,出售的物品是 (Pencil, Bible),按词典序排列,并用逗号分隔。
对于2020-06-02,出售的物品是 (Mask),只需返回该物品名。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/group-sold-products-by-the-date
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
count + group_concat + separator
select
sell_date,
// 统计数量
count(distinct product) as num_sold,
// group_concat将组中的字符串连接
// separator 使用指定符号隔开
group_concat(distinct product order by product separator ',') as products
from activities
group by sell_date
order by sell_date;
边栏推荐
猜你喜欢
海量服务实例动态化管理
KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)
Pisanix v0.2.0 released | Added support for dynamic read-write separation
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
树表的查找
【 2 】 OpenCV image processing: basic knowledge of OpenCV
线性表的查找
2022 EdgeX中国挑战赛8月3日即将盛大开幕
学习笔记-----左偏树
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
随机推荐
学习笔记-----左偏树
select tag custom style
C语言日记 9 if的3种语句
[ROS] (10) ROS Communication - Service Communication
HOG feature study notes
如何看待自己的羞愧感
转:查尔斯·汉迪:你是谁,比你做什么更重要
What should I do if the self-incrementing id of online MySQL is exhausted?
KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)
《.NET物联网从零开始》系列
C language diary 9 3 kinds of statements of if
Greenplum数据库故障分析——能对数据库base文件夹进行软连接嘛?
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
【OpenCV 图像处理2】:OpenCV 基础知识
Domain Driven Design - MDD
C学生管理系统 指定位置插入学生节点
LPQ (local phase quantization) study notes
CPDA|运营人如何从负基础学会数据分析(SQL)
Using OpenVINO to implement the flying paddle version of the PGNet inference program
SDC简介