Leetcode 1484题: Group Sold Products By The Date

题目描述

原始表有两个column,一个是产品的售卖日期,另一个是具体的产品名称。

样例表

Leetcode 1484题: Group Sold Products By The Date

题目要求:

Write an SQL query to find for each date, the number of distinct products sold and their names.

预期结果:

Leetcode 1484题: Group Sold Products By The Date

My Answer

其实这道题难度不大,唯一值得一提的就是如何在mysql进行字符串的拼接。我使用的是group_concat()函数,即对分组后的结果进行拼接。在函数中,我们可以自行定义拼接的符号sepearator,默认为,

下面是代码

select sell_date,
      count(distinct product) num_sold, 
      group_concat(distinct product) products 
      from Activities 
      group by sell_date;
上一篇:1081. Smallest Subsequence of Distinct Characters


下一篇:Elasticsearch教程(32) ES 聚合查询后过滤 Distinct Group By Having功能