当前位置:网站首页>数据库每日一题---第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
五、测试结果



边栏推荐
- js判断浏览器是否打开了控制台
- tp5.1上传excel文件并读取其内容
- OJ每日一练——验证子串
- canvas生成海报
- After passing the hcip exam, I still failed to change my career. What do professional network workers value most
- Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
- Tianyi cloud takes advantage of the new infrastructure to build a "4+2" capability system for digital transformation
- 事物系统的几种异常场景
- 【GO】go mod模式, package 12import/add is not in GOROOT
- RedisTemplate使用遇到\x00的問題
猜你喜欢

MySQL-Seconds_behind_master 的精度误差

同步电路与跨时钟域电路设计2——多bit信号的跨时钟域传输(FIFO)

考过HCIP依然转行失败,职业网工最看重的到底是什么

弱电转职业网工难不难?华为售前工程师分享亲身经历

Digital data depth | about software self-control, source code left, no code right

Flutter outsourcing, undertaking flutter project

Canvas generate Poster

OJ每日一练——过滤多余的空格

OLAP ——Druid简介

07 项目成本管理
随机推荐
OJ每日一练——过滤多余的空格
[go] go array and slice (dynamic array)
Asynchronous FIFO
OJ每日一练——单词的长度
周国华 读书随记
[kubernetes series] overview of kubernetes
OJ daily practice - class dining
10 Super VIM plug-ins, I can't put them down
反向代理HAProxy
RedisTemplate使用遇到\x00的問題
优化——线性规划
KunlunDB备份和恢复
[go] getting started with go modules
JSBridge
XML escape character cross reference table
Sword finger offer 07 Rebuild binary tree
2. interface (calculator)
js----SVG转PNG
双重跨域:Access-Allow-Origin header contains multiple values“*, *”,but only one is allowed
wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz