SQL 1484 Group Sold Products By The Date

2022/9/15 2:18:42

本文主要是介绍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


这篇关于SQL 1484 Group Sold Products By The Date的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程