I have a table with three columns.
For each id we have as many as 400 index values. I want to add columns based on the number of index. In the example which I provide, I have 4 index, then I add four columns to the table. Here is the table I have:
Create table  buy_sell (id int, idx varchar(255), sell float(2, 1));
insert into buy_sell (id, idx,  sell) values ('1', 'a',  '4');
insert into buy_sell (id, idx,  sell) values ('1', 'b',  '6');
insert into buy_sell (id, idx,  sell) values ('1', 'c',  '8');
insert into buy_sell (id, idx,  sell) values ('1', 'd',  '9');
insert into buy_sell (id, idx,  sell) values ('3', 'b ', '1');
insert into buy_sell (id, idx,  sell) values ('3', 'c ', '2');
insert into buy_sell (id, idx,  sell) values ('2', 'a',  '5');
insert into buy_sell (id, idx,  sell) values ('2', 'b',  '7');
insert into buy_sell (id, idx,  sell) values ('2', 'd',  '5');
SELECT * FROM buy_sell;
Here is the result:
| id | idx | sell | 
|---|---|---|
| 1 | a | 4.0 | 
| 1 | b | 6.0 | 
| 1 | c | 8.0 | 
| 1 | d | 9.0 | 
| 3 | b | 1.0 | 
| 3 | c | 2.0 | 
| 2 | a | 5.0 | 
| 2 | b | 7.0 | 
| 2 | d | 5.0 | 
So, for example for id=1, we have four index here (a, b, c, d) and then we have four non-zero columns. For id = 3, we have two index (b, c), then we have two non-zero columns, so for column 1, we put zero, for column 2 we put the 1, and for column 3 we put the 2. And so on. Here is the table that I want:
| id | sell1 | sell2 | sell3 | sell4 | 
|---|---|---|---|---|
| 1 | 4 | 6 | 8 | 9 | 
| 3 | 0 | 1 | 2 | 0 | 
| 2 | 5 | 7 | 0 | 5 | 
I searched a lot, and tried Group_concat, JSON_ARRAYAGG, etc, but I can't find out how to solve this. What do I need to do?