0

My Original Data :

id   | GRP_ID | VAL_1  | VAL_2   | VAL_3  |  TYPE
---------------------------------------------------
1    |   R2   |  B01   |         |        | COL_B
2    |   R2   |        |   A01   |        | COL_A
3    |   R3   |  B02   |         |        | COL_B
4    |   R1   |        |   C01   |        | COL_C
5    |   R2   |  C02   |         |        | COL_C
6    |   R3   |        |         |   C03  | COL_C

Then i modified like this :

-----------------------------------------
GRP_ID  | group_CONCAT1      | group_CONCAT2         
-------------------------------------------
 R1     |  COL_C             | C01
 R2     |  COL_A,COL_B,COL_C | A01,B01,C02
 R3     |  COL_B,COL_C       | B02,C03

And i want get result like this :

GRP_ID  |COL_A  |  COL_B   | COL_C
----------------------------------
 R1     | ---   |   ---    | C01
 R2     | A01   |   B01    | C02
 R3     | ---   |   B02    | C03
Mortred
  • 1
  • 1
  • What's the logic for deciding that the column order in the output is a,b,c? Are you imposing an alpha sort or is it simply the order of the longest group_concat1 in source? (which may or may not be ordered -you don't say). Also would it not be easier to go to source and skip the group_concats which force you to split strings? – P.Salmon Sep 14 '22 at 07:17
  • What's your problem – flyingfox Sep 14 '22 at 07:34
  • Why is `A01`, `B01` and `C02` in the same line? Please post the query you use to get the `GROUP_CONCAT` results. – FanoFN Sep 14 '22 at 07:38
  • @p.salmon the result column name is not ordered just from split string – Mortred Sep 14 '22 at 07:38
  • @FanoFN cause it have same sub_id on key of group by – Mortred Sep 14 '22 at 07:42
  • There's no `sub_id` column mentioned in your data example above. I suggest you check with the [linked duplicate question](https://stackoverflow.com/questions/12598120/mysql-pivot-table-query-with-dynamic-columns) and see if any answer there solves your issue. If not, please update your question with more details - include `sub_id` that you mentioned and the query you've used to get the `GROUP_CONCAT()` results. Then we'll see if it really is a different issue, we'll vote to reopen your question. – FanoFN Sep 14 '22 at 08:03

0 Answers0