Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Aggregation in sqlite

Anyone knows if String Aggregation in sqlite is possible? If i have an animal column with 5 rows/datas, how can i combine them so that the output would be in one field 'dog','cat','rat','mice','mouse' as animals

Thanks

like image 289
monmonja Avatar asked Aug 18 '10 18:08

monmonja


People also ask

What is string aggregation?

STRING_AGG is an aggregate function that takes all expressions from rows and concatenates them into a single string. Expression values are implicitly converted to string types and then concatenated. The implicit conversion to strings follows the existing rules for data type conversions.

How can we concatenate string in SQLite?

The SQL standard provides the CONCAT() function to concatenate two strings into a single string. SQLite, however, does not support the CONCAT() function. Instead, it uses the concatenate operator ( || ) to join two strings into one.

How do I group data in SQLite?

SQLite GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups. GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause.

What do you understand by SQLite aggregate functions?

SQLite Aggregate functions are the functions where values of multiple rows are grouped together as input on certain criteria and form a single value as output. Following is a list of some SQLite Aggregate function: SQLite MIN Function. SQLite MAX Function.


1 Answers

You're looking for something like the following:

select group_concat(animal) from animals;

This will return something like the following:

dog,cat,rat,mice,mouse

If you don't want to use a comma as the separator, you can add your own separator as a second parameter:

select group_concat(animal, '_') from animals;

which will return:

dog_cat_rat_mice_mouse
like image 175
Mark Avatar answered Nov 02 '22 19:11

Mark