Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite3 - Merge multiple rows and multiple column into one column without duplicates

Tags:

sql

sqlite

I has a problem:

In my SQLite (sqlite3 on android) database I have a table like so:

idImp  | cer  | caus
---------------------
id1    | 010  | D1
id1    | 010  | D2
id1    | 011  | D2
id1    | 011  | D3
id2    | 010  | D1
id2    | 010  | D2
id2    | 011  | D2
id2    | 011  | D4

and I'd like to get to

idImp | Conc
id1   | '010-D1-D2;011-D2-D3;'
id2   | '010-D1-D2;011-D2-D4;'

Using SQLite is it possible?? Does anyone have an idea how to make this work?

Thanks!

EDIT (SRY, There was an error in the result table) :

What I need is a SQL query that concatenates the fields "caus" and the field "cer" with specific separator grouped by "idImp".

I found in the documentation of sqlite group_concat (X, Y) that work on single column as it serves me, but I need to concatenate multiple columns and i don't know how to do...

like image 467
user2961118 Avatar asked Feb 15 '23 18:02

user2961118


1 Answers

Group the values in two steps:

SELECT idImp,
       group_concat(causConc, '') AS Conc
FROM (SELECT idImp,
             cer || '-' || group_concat(caus, '-') || ';' AS causConc
      FROM MyTable
      GROUP BY idImp,
               cer)
GROUP BY idImp
like image 100
CL. Avatar answered Feb 17 '23 08:02

CL.