0

combine value of a column if the correspondence in the other column repeated values.

The result should be 21,22,23 (Column A having repeated values 1)

24 (Column A having repeated values 2)

SELECT coulmn B from table group by column A 

See below table

Column A Column B
1 21
1 22
1 23
2 24
asdasd
  • 1
  • 2
  • does this answer your question https://stackoverflow.com/questions/13639262/optimal-way-to-concatenate-aggregate-strings – George Joseph Jan 04 '22 at 06:52
  • 1
    I do not understand the question, please provide some `raw data` (before the wanted query) and then the `expected result`. Also "sql" by itself is NOT a good tag to use, we need to know which database vendor is involved, please add the database as a tag (e.g. MySQL) – Paul Maxwell Jan 04 '22 at 06:54

1 Answers1

0

I am not sure what database technology you are working on however this can be achieved pretty easily using group_concat feature in mysql

mysql>
mysql> create table t1(id int,value int);
Query OK, 0 rows affected (0.03 sec)

mysql>
mysql>
mysql> insert into t1 values(1,21),(1,22),(1,23),(2,24);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql>
mysql> select * from t1;
+------+-------+
| id   | value |
+------+-------+
|    1 |    21 |
|    1 |    22 |
|    1 |    23 |
|    2 |    24 |
+------+-------+
4 rows in set (0.00 sec)

mysql>
mysql>
mysql>
mysql> select id,group_concat(value) from t1 group by id;
+------+---------------------+
| id   | group_concat(value) |
+------+---------------------+
|    1 | 21,22,23            |
|    2 | 24                  |
+------+---------------------+
2 rows in set (0.00 sec)
SelvaRaj B
  • 96
  • 2
  • I want this in commerce_order_item.title As my query is SELECT commerce_order.order_id, commerce_order.mail, commerce_order.total_price__number, commerce_order.changed, commerce_order_item.title FROM commerce_order LEFT JOIN commerce_order_item ON commerce_order.order_id = commerce_order_item.order_id WHERE cart = 1 AND commerce_order.changed BETWEEN $startdate AND $endates – asdasd Jan 04 '22 at 07:35