4

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...

4

1 回答 1

8

分两步对值进行分组:

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
于 2013-11-06T21:54:40.643 回答