Aggregate function GROUP_CONCAT(expr) in MySQL

Antonio Mailtraq

I have problem in using the aggregate function GROUP_CONCAT(expr) in MySQL database: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

I have this table with this rows:

+------+------+
| NAME | AREA |
+------+------+
| and  | DM4  |
| ant  | DMS  |
| don  | DM7  |
| ett  | DM4  |
| fab  | DM4  |
| fra  | DMS  |
| gia  | DM7  |
| gug  | DM2  |
| raf  | DM   |
| rob  | DM2  |
| vin  | DM7  |
+------+------+
16 rows in set

And I tried aggregate query to obtain this output where the name raf should be present in all areas because your area is DM:

+------+------------------------------+
| AREA | group_emails                 |
+------+------------------------------+
| DM4  | and; ant; ett; fab; fra; raf |
| DM6  | and; ant; raf                |
| DM7  | and; don; gia; vin; raf      |
| DM2  | gug; rob; raf                |
| DMS  | ant; fra ; raf               |
+------+------------------------------+

I have tested this solution but the output is wrong.

This is beginning to make me believe my structure as a whole is not correct.

What am I missing ?

I would greatly appreciate any help you can give me in working this problem.

mysql> SELECT
    AREA,
    GROUP_CONCAT(
        DISTINCT NAME
        ORDER BY
            NAME ASC SEPARATOR '; '
    ) AS group_emails
FROM
    `tbl_NAMES`
GROUP BY
        AREA
ORDER BY
    NAME ASC;
+------+-------------------------+
| AREA | group_emails            |
+------+-------------------------+
| DM4  | and; ant; ett; fab; fra |
| DM   | raf                     |
| DM6  | and; ant                |
| DM7  | and; don; gia; vin      |
| DM2  | gug; rob                |
| DMS  | ant; fra                |
+------+-------------------------+
6 rows in set
Bohemian

You need to base your data on a union of a straight select with a join to itself using a LIKE comparison to collect all the names that "match":

SELECT
    AREA,
    GROUP_CONCAT(
        DISTINCT NAME
        ORDER BY NAME
        SEPARATOR '; ') AS group_emails
FROM (
SELECT * FROM tbl_NAMES
UNION
SELECT t2.name, t1.AREA
FROM tbl_NAMES t1
LEFT JOIN tbl_NAMES t2
  ON t1.AREA like concat(t2.AREA, '%')
) x
GROUP BY AREA
ORDER BY group_emails

See SQLFiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL: Aggregate function: group by joined variable

From Dev

Aggregate function GROUP_CONCAT(expr) in MySQL

From Dev

Spark SQL replacement for MySQL's GROUP_CONCAT aggregate function

From Dev

aggregate function by time and by group

From Dev

MySQL Case Statement with aggregate function

From Dev

MySQL- Misuse of aggregate function

From Dev

Grouping by aggregate function max in MySQL

From Dev

Postgres "group by" on aggregate function(s)

From Dev

Aggregate function in group by multiple columns

From Dev

Aggregate data with custom group function

From Dev

invalid : aggregate function or the GROUP BY clause

From Dev

Does MySQL have a way to "coalesce" as an aggregate function?

From Dev

Switched MySQL to Postgres: "column must appear in the GROUP BY clause or be used in an aggregate function"

From Dev

MySQL aggregate function against different groups which should be computed from an outermost GROUP BY?

From Java

must appear in the GROUP BY clause or be used in an aggregate function

From Dev

Is it possible to group values in a map in an aggregate function with MongoDB?

From Dev

Applying aggregate function SUM together with GROUP BY

From Dev

Why GROUP BY is needed for a nested aggregate function in Oracle

From Dev

Column is not contained in either an aggregate function or the GROUP BY clause

From Dev

Multiple INNER JOIN with GROUP BY and Aggregate Function

From Dev

add where condition in aggregate and group function in mongodb

From Dev

How to use aggregate function without GROUP BY?

From Dev

over partition by without an aggregate function, avoiding group by

From Dev

Why GROUP BY is needed for a nested aggregate function in Oracle

From Dev

Group rows without invoking an aggregate function

From Dev

The aggregate function GROUP BY and SUM return inconsisten data?

From Dev

How to use aggregate function without GROUP BY?

From Dev

over partition by without an aggregate function, avoiding group by

From Dev

How to combine Join with an aggregate function and group by

Related Related

  1. 1

    MySQL: Aggregate function: group by joined variable

  2. 2

    Aggregate function GROUP_CONCAT(expr) in MySQL

  3. 3

    Spark SQL replacement for MySQL's GROUP_CONCAT aggregate function

  4. 4

    aggregate function by time and by group

  5. 5

    MySQL Case Statement with aggregate function

  6. 6

    MySQL- Misuse of aggregate function

  7. 7

    Grouping by aggregate function max in MySQL

  8. 8

    Postgres "group by" on aggregate function(s)

  9. 9

    Aggregate function in group by multiple columns

  10. 10

    Aggregate data with custom group function

  11. 11

    invalid : aggregate function or the GROUP BY clause

  12. 12

    Does MySQL have a way to "coalesce" as an aggregate function?

  13. 13

    Switched MySQL to Postgres: "column must appear in the GROUP BY clause or be used in an aggregate function"

  14. 14

    MySQL aggregate function against different groups which should be computed from an outermost GROUP BY?

  15. 15

    must appear in the GROUP BY clause or be used in an aggregate function

  16. 16

    Is it possible to group values in a map in an aggregate function with MongoDB?

  17. 17

    Applying aggregate function SUM together with GROUP BY

  18. 18

    Why GROUP BY is needed for a nested aggregate function in Oracle

  19. 19

    Column is not contained in either an aggregate function or the GROUP BY clause

  20. 20

    Multiple INNER JOIN with GROUP BY and Aggregate Function

  21. 21

    add where condition in aggregate and group function in mongodb

  22. 22

    How to use aggregate function without GROUP BY?

  23. 23

    over partition by without an aggregate function, avoiding group by

  24. 24

    Why GROUP BY is needed for a nested aggregate function in Oracle

  25. 25

    Group rows without invoking an aggregate function

  26. 26

    The aggregate function GROUP BY and SUM return inconsisten data?

  27. 27

    How to use aggregate function without GROUP BY?

  28. 28

    over partition by without an aggregate function, avoiding group by

  29. 29

    How to combine Join with an aggregate function and group by

HotTag

Archive