当前位置:网站首页>数据库每日一题---第20天:按日期分组销售产品
数据库每日一题---第20天:按日期分组销售产品
2022-06-22 21:33:00 【InfoQ】
一、问题描述
Activities+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| sell_date | date |
| product | varchar |
+-------------+---------+
此表没有主键,它可能包含重复项。
此表的每一行都包含产品名称和在市场上销售的日期。
SQLsell_date二、题目要求
样例
输入:
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),只需返回该物品名。
考察
1.聚合函数、逻辑分析
2.建议用时10~25min
三、问题分析
group_concat,四、编码实现
select sell_date,count(distinct product) as 'num_sold',
group_concat(distinct product order by product separator ',') as 'products'
from Activities
group by sell_date
order by sell_date
五、测试结果



边栏推荐
- 剑指 Offer 11. 旋转数组的最小数字
- js图片分辨率压缩
- 好东西要分享啦
- What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
- Reverse proxy haproxy
- 优化——线性规划
- Tp5.1 upload excel file and read its contents
- [PHP] PHP polymorphism
- KunlunDB查询优化(二)Project和Filter下推
- OJ daily practice - delete word suffixes
猜你喜欢

OLAP ——Druid简介
![[go] go array and slice (dynamic array)](/img/63/9a3fb70b202ca45828cd1b62897eec.jpg)
[go] go array and slice (dynamic array)

KunlunDB查询优化(二)Project和Filter下推

昆仑分布式数据库Sequence功能及其实现机制

事务系统的隔离级别

Enjoy high-performance computing! Here comes the Tianyi cloud HPC solution

【GO】Go Modules入門

Digital data was invited to participate in Nantong enterprise digital transformation Seminar

Leakcanary source code (2)

事物系统的几种异常场景
随机推荐
Notes on zhouguohua's reading
Flutter outsourcing, undertaking flutter project
three.js模拟驾驶游览艺术展厅---打造超级相机控制器
[go] getting started with go modules
Reddit's discussion on lamda model: it is not stateless. It adopts a dual process. Compared with the way it edits Wikipedia, it doesn't matter whether it has feelings or not
Array and string offset access syntax with curly braces is no longer support
包管理工具--NPM、--CNPM、 --Yarn、 --CYarn
xml转义字符对照表
OJ daily practice - Verifying substring
Sword finger offer 11 Minimum number of rotation array
wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz
Tp5.1 upload excel file and read its contents
再立云计算“昆仑”,联想混合云Lenovo xCloud凭什么?
Bubble sort pointer
07 项目成本管理
剑指 Offer 11. 旋转数组的最小数字
Use smart doc to automatically generate interface documents
OJ daily practice - delete word suffixes
【首发】请求一下子太多了,数据库危
[go] go language interface