📊 Efficient SQL Solution for Grouped Product Count and Concatenation 🏆 (Beats 91% in Speed!)
\uD83D\uDCCA SQL Solution for Grouping and Counting Sold Products \uD83C\uDFC6\n\n### Intuition\n Describe your first thoughts on how to solve this problem. \nT
## \uD83D\uDCCA SQL Solution for Grouping and Counting Sold Products \uD83C\uDFC6\n\n### Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to find the number of unique products sold for each date and list them in lexicographical order.\n\n### Approach\n<!-- Describe your approach to solving the problem. -->\n1. Use `GROUP BY` to group entries by each `sell_date`.\n2. Apply `COUNT(DISTINCT product)` to count unique products sold for each date.\n3. Use `GROUP_CONCAT` with `DISTINCT` and `ORDER BY` to concatenate product names in lexicographical order.\n\n### Complexity\n- **Time complexity:** O(n), where n is the number of entries in the `Activities` table.\n- **Space complexity:** O(n), for storing the grouped results.\n\n# Code\n```mysql []\n# Write your MySQL query statement below\nSELECT \n    sell_date,\n    COUNT(DISTINCT product) AS num_sold,\n    GROUP_CONCAT(DISTINCT product ORDER BY product ASC) AS products\nFROM Activities\nGROUP BY sell_date\nORDER BY sell_date;\n```\n\nThis query effectively groups and counts unique products for each date, ensuring product names are listed alphabetically.\n\nI welcome any feedback or suggestions for optimizing the code!
['Database', 'MySQL']