Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group different rows in one by combining strings

Tags:

sqlite

I have a data set like this:

Column1  Column2    1       A    1       B    1       C    2       D    2       E    2       F    2       G    3       H    3       I  

and I would like to merge it into something like this:

Column1  Column2    1       A, B, C    2       D, E, F, G    3       H, I 

Is it possible to do this in SQLite somehow? I though of GROUP BY Column1, but I don't see how I can combine the Column2 data in one string...

Thanks!

like image 910
georgeb89 Avatar asked Oct 13 '10 17:10

georgeb89


People also ask

How do I combine multiple rows in Excel into one row?

Select the range of cells where you want to merge rows. Go to the Ablebits Data tab > Merge group, click the Merge Cells arrow, and then click Merge Rows into One.

How do I combine multiple strings into one?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.


1 Answers

 SELECT Column1, group_concat(Column2) FROM Table GROUP BY Column1 

group_concat takes an optional second argument (a string) to use as the concatenation separator if you don't want a single ',' character.

like image 189
Larry Lustig Avatar answered Oct 12 '22 22:10

Larry Lustig