3

I have a table with a one to many mapping from ID to some_value. What query can give me all of some_value for a particular ID?

I can use any array, string, or other object useful in SQL as the final result in list_of_values. You don't need to use my comma seperated format.

My particular varient of SQL is Hive, but the syntax and functions are very similar to MySQL :)

Starting table:

ID             some_value
-----------    ------------
1              a
1              b
1              c
2              x
2              y
2              z
2              a
3              g
3              h

Finished table:

ID              list_of_values
----------      ----------------
1               a, b, c
2               x, y, z, a
3               g, h
Don P
  • 60,113
  • 114
  • 300
  • 432

2 Answers2

3
SELECT ID, COLLECT_SET(some_value) AS list_of_values
    FROM YourTable 
    GROUP BY ID;
Joe Stefanelli
  • 132,803
  • 19
  • 237
  • 235
0
SELECT
    `id`,
    GROUP_CONCAT(DISTINCT `some_value` SEPARATOR ', ') AS 'list_of_values'
FROM `your_table`
GROUP BY `id`
inhan
  • 7,394
  • 2
  • 24
  • 35