Group Sold Products By The Date | MySQL
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to find the name and the number of different products sold on each date.\n\n# A
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to find the name and the number of different products sold on each date.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Select specific columns from the table.\n\n    > `SELECT` \u2192 This command retrieves data from a database\n\n    > `AS` \u2192 This command renames a column with an alias (temporary name). In most database languages, we can skip the `AS` keyword and get the same result\n\n\n2. Count how many different products were sold on each date.\n\n    > `COUNT()` \u2192 This function returns the number of rows\n\n    > `DISTINCT` \u2192 This statement returns only distinct (different) values\n\n3. Concatenate the product names in lexicographical order for each date.\n\n    > `GROUP_CONCAT()` \u2192 This function concatenates and aggregates values from multiple rows within a specific column into a single string\n\n    > `ORDER BY` \u2192 This command sorts the result-set in ascending (smallest to largest) order by default\n\n4. Group and arrange the results to show the number of products sold on each date in chronological order.\n\n    > `GROUP BY` \u2192 This command groups rows that have the same values into summary rows, typically to perform aggregate functions on them\n\n# Complexity\n- Time complexity: $O(n log n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n`n` is the number of rows in the table. This is because the query involves sorting all the products in the worst case.\n\n# Code\n```\nSELECT \n    sell_date, \n    COUNT(DISTINCT product) AS num_sold,\n    GROUP_CONCAT(DISTINCT product ORDER BY product) AS products\nFROM Activities\nGROUP BY sell_date\nORDER BY sell_date;\n```
['MySQL']