xinyu04

导航

SQL 1484 Group Sold Products By The Date

Table Activities:

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| sell_date   | date    |
| product     | varchar |
+-------------+---------+

There is no primary key for this table, it may contain duplicates.
Each row of this table contains the product name and the date it was sold in a market.

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

The sold products names for each date should be sorted lexicographically.

Return the result table ordered by sell_date.

Solution

  • 统计不同的数量,这里利用 \(count(distinct)\)
  • 将多个字符串连接:\(group\_concat\)
  • 注意是先 \(group\ by\) 然后再 \(order\ by\)
点击查看代码
# Write your MySQL query statement below
select a.sell_date,
    count(distinct a.product) as num_sold,
    group_concat(
        distinct a.product order by a.product separator ','
    ) as products
from Activities a
group by a.sell_date
order by a.sell_date

posted on 2022-09-14 06:17  Blackzxy  阅读(30)  评论(0编辑  收藏  举报