Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL group concat with duplicate ID's

I am trying to concat the week_num from a table, grouping bby Driver_id. But group_concat will not keep duplicate ID’s, but My requirement is to have them.
Is there any way to achieve this?

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can try to use subquery & self-join

Query 1:

SELECT t2.*,t1.Consolidate_weeks
FROM (
    SELECT Driver_id,GROUP_CONCAT(Week_num) Consolidate_weeks
    FROM T
    GROUP BY Driver_id
) t1 INNER JOIN T t2
ON t1.Driver_id = t2.Driver_id

Results:

| Driver_id | Week_num | Consolidate_weeks |
|-----------|----------|-------------------|
|         1 |   Week 1 |     Week 1,Week 2 |
|         2 |   Week 1 |     Week 1,Week 2 |
|         3 |   Week 1 |     Week 1,Week 2 |
|         4 |   Week 1 |     Week 1,Week 2 |
|         1 |   Week 2 |     Week 1,Week 2 |
|         2 |   Week 2 |     Week 1,Week 2 |
|         3 |   Week 2 |     Week 1,Week 2 |
|         4 |   Week 2 |     Week 1,Week 2 |
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading