I have a really peculiar task of creating a SELECT
with following conditions:
Source table is as follows:
+----------------+
| source |
+-----+----------+
| id | values |
+-----+----------+
| 1 | 1,2,3 |
+-----+----------+
| 3 | 4,5,6 |
+-----+----------+
| 8 | 1,4,7 |
+-----+----------+
| 9 | 8,9 |
+-----+----------+
| 21 | 10,11,12 |
+-----+----------+
| 69 | 11,45,89 |
+-----+----------+
And the result after some kind of SELECT
would be
+--------------------------+
| target |
+-------+------------------+
| sid | values |
+-------+------------------+
| 1,3,8 | 1,2,3,4,5,6,7 |
+-------+------------------+
| 9 | 8,9 |
+-------+------------------+
| 21,69 | 10,11,12,45,89 |
+-------+------------------+
Meaning it concatenates all the ids and values, where any of the set values have an element in common.
Does anybody have any idea how to do it? Be it in MySQL, or SQL Server. Ideally without functions/stored procedures.