MS SQL
Question\n\n1484. Group Sold Products By The Date\nEasy\n\nSQL Schema\nTable Activities:\n\n+-------------+---------+\n| Column Name | Type    |\n+-------------
#### Question\n```\n1484. Group Sold Products By The Date\nEasy\n\nSQL Schema\nTable Activities:\n\n+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| sell_date   | date    |\n| product     | varchar |\n+-------------+---------+\nThere is no primary key for this table, it may contains duplicates.\nEach row of this table contains the product name and the date it was sold in a market.\n \n\nWrite an SQL query to find for each date, the number of distinct products sold and their names.\n\nThe sold-products names for each date should be sorted lexicographically. \n\nReturn the result table ordered by sell_date.\n\nThe query result format is in the following example.\n\nActivities table:\n+------------+-------------+\n| sell_date  | product     |\n+------------+-------------+\n| 2020-05-30 | Headphone   |\n| 2020-06-01 | Pencil      |\n| 2020-06-02 | Mask        |\n| 2020-05-30 | Basketball  |\n| 2020-06-01 | Bible       |\n| 2020-06-02 | Mask        |\n| 2020-05-30 | T-Shirt     |\n+------------+-------------+\n\nResult table:\n+------------+----------+------------------------------+\n| sell_date  | num_sold | products                     |\n+------------+----------+------------------------------+\n| 2020-05-30 | 3        | Basketball,Headphone,T-shirt |\n| 2020-06-01 | 2        | Bible,Pencil                 |\n| 2020-06-02 | 1        | Mask                         |\n+------------+----------+------------------------------+\nFor 2020-05-30, Sold items were (Headphone, Basketball, T-shirt), we sort them lexicographically and separate them by comma.\nFor 2020-06-01, Sold items were (Pencil, Bible), we sort them lexicographically and separate them by comma.\nFor 2020-06-02, Sold item is (Mask), we just return it.\n\n```\n\n\n#### Answer\n```SQL\n\n/* Write your T-SQL query statement below */\nselect sell_date, count(product) num_sold, STRING_AGG(product,\',\') WITHIN GROUP(order by product) as products from (select distinct * from Activities) as a \ngroup by sell_date\norder by sell_date\n\n```
['MS SQL Server']