Question: I have a table `SUBSCRIBER` and in it the data is stored in a way that `bars` is a multi-valued column . Different values of bars are stored in different rows with the `ID` as following :

```none
ID BARS
1 , BAR1
1 , BAR2
1 , BAR3
2 , BAR4
2 , BAR3
3 , BAR3
4 , BAR1
4 , BAR4

```

OUTPUT for the data set should be the following:

```none
1,BAR1,
2,,BAR4
3,,,
4,BAR1,BAR4

```

So the query should be extracting 2 particular BAR values from the multi valued column(BARS) and gives the result for a report based on the conditions as following:

```none
ID,BAR1,BAR4 : WHEN BOTH BAR1 AND BAR4 EXIST
ID,BAR1,, : WHEN BAR1 EXISTS AND BAR4 IS NOT PRESENT
ID,,BAR4 : WHEN BAR4 EXISTS AND BAR1 IS NOT PRESENT
ID,,, : WHEN BOTH BAR1 AND BAR4 ARE NOT PRESENT

```

Answer: 
If you don't need the extra commas, then:

```
SELECT CONCAT(id, ',',
          GROUP_CONCAT(bars ORDER BY bars))
    FROM subscriber
    WHERE bars IN ('bar1', 'bar4')
    GROUP BY id;

```

If you do need the commas, then search for "pivoting".
You can use `GROUP_CONCAT` and `CASE`:

**Query:**

```
SELECT 
    SUBSCRIBER.ID,
    CASE
        WHEN GROUP_CONCAT(CASE WHEN BARS='BAR1' OR BARS='BAR4' THEN BARS END) LIKE '%BAR1%' OR GROUP_CONCAT(CASE WHEN BARS='BAR1' OR BARS='BAR4' THEN BARS END) LIKE '%BAR4%' THEN GROUP_CONCAT(CASE WHEN BARS='BAR1' OR BARS='BAR4' THEN BARS END)
    ELSE
        GROUP_CONCAT(",") END AS BARS
FROM test.SUBSCRIBER
GROUP BY SUBSCRIBER.ID;

```

**Test:**

```
mysql> SELECT 
    -> SUBSCRIBER.ID,
    -> CASE
    -> WHEN GROUP_CONCAT(CASE WHEN BARS='BAR1' OR BARS='BAR4' THEN BARS END) LIKE '%BAR1%' OR GROUP_CONCAT(CASE WHEN BARS='BAR1' OR BARS='BAR4' THEN BARS END) LIKE '%BAR4%' THEN GROUP_CONCAT(CASE WHEN BARS='BAR1' OR BARS='BAR4' THEN BARS END)
    -> ELSE
    -> GROUP_CONCAT(",") END AS BARS
    -> FROM test.SUBSCRIBER
    -> GROUP BY SUBSCRIBER.ID;
+----+-----------+
| ID | BARS      |
+----+-----------+
|  1 | BAR1      |
|  2 | BAR4      |
|  3 | ,         |
|  4 | BAR1,BAR4 |
+----+-----------+
4 rows in set (0.00 sec)

mysql> 

```