在SQLite中使用GROUP_CONCAT函数内的ORDER BY子句

时间:2012-03-23 01:45:53

标签: sql sqlite group-concat

我不认为我可以在ORDER BY函数中使用GROUP_CONCAT子句。

有谁知道在SQLite中完成此行为的棘手方法?

之前我看过这个question。但我有一个复杂的查询。

我的陈述如下:

SELECT
    c.col1, c.col3, m.col3, m.col4,
    count(m.col1), count(re.col2) AS cnt,
    GROUP_CONCAT(p.col1 ORDER BY p.col1) AS "Group1",
    GROUP_CONCAT(p.col2 ORDER BY p.col1) AS "Group2", 
    GROUP_CONCAT(CASE WHEN con.col3 is null THEN p.col1 ELSE con.col3 END),
    con.col4, con.col5, p.col3
FROM t1 re
    INNER JOIN t2  c  ON (re.col1  = c.col1)
    INNER JOIN t3  p  ON (re.col2  = p.col1)
    LEFT JOIN  t4 con ON (con.col1 = p.col2)
    INNER JOIN  t5 m  ON (m.col1   = c.col5) 
GROUP BY re.col1 

Group1Group2来自同一个表但不同的列:我想保留Group1Group2的顺序:

table t3 
+------+------+
| col1 | col2 |
+------+------+
|    1 | A    |
|    2 | B    |
|    3 | C    |
|    4 | D    |
|    5 | E    |
+------+------+

所以,如果显示Group12,1,3 Group2应显示为B,A,C

4 个答案:

答案 0 :(得分:8)

SQLite不支持ORDER BY中的GROUP_CONCAT,但您实际上可以伪造它:

GROUP_CONCAT(list_order || ':' || value)

然后你需要在代码中拆分结果,以便找回你的排序和价值。

答案 1 :(得分:1)

为避免不确定性,您可以使用递归CTE,如下所示:

sqlite> create table t3(pos,col1,col2);
sqlite> insert into t3 values(1,2,'B'),(2,1,'A'),(3,5,'E');
sqlite> select * from t3;
1|2|B
2|1|A
3|5|E
sqlite>
with
  sorted(pos,c1,c2) as (
    select row_number() over (order by t3.pos), -- sorting by first column's value
      t3.col1, t3.col2
      from t3
  ),
  concat(pos,c1,c2) as (
    select sorted.pos,sorted.c1,sorted.c2  -- starting with values for first position
      from sorted
     where sorted.pos=1
     union all
    select sorted.pos,
           concat.c1||','||sorted.c1,  -- adding next value from col1
           concat.c2||','||sorted.c2   -- adding next value from col2
      from concat
      join sorted
        on concat.pos+1 = sorted.pos   -- going through subsequent positions
  )
select c1, c2
  from concat
 order by pos desc
 limit 1;  -- order by desc limit 1 means 'take the row with largest number'

2,1,5|B,A,E

尽管非常复杂,但此解决方案可确保正确排序,并且可以轻松扩展更多列。排序列可能有空白-sorted CTE会使其变成适当的整数序列。

请注意,row_number() over (order by...)可能需要支持窗口功能的最新版本的sqlite。

答案 2 :(得分:0)

这样的事情怎么样?

SELECT 
    col1, col3, col3, col4,
    count(col1), count(re.col2) AS cnt,
    GROUP_CONCAT(p.col1) AS "Group1",
    GROUP_CONCAT(p.col2) AS "Group2", 
    GROUP_CONCAT(CASE WHEN con.col3 is null THEN p.col1 ELSE con.col3 END),
    con.col4, con.col5, p.col3
FROM (
    SELECT 
        *
    FROM t1 re
        INNER JOIN t2  c  ON (re.col1  = c.col1)
        INNER JOIN t3  p  ON (re.col2  = p.col1)
        LEFT JOIN  t4 con ON (con.col1 = p.col2)
        INNER JOIN  t5 m  ON (m.col1   = c.col5) 
    ORDER BY
        p.col1 ASC,
        p.col2 ASC
)
GROUP BY re.col1 

我已经测试了它,但是如果你可以分享一些数据......

答案 3 :(得分:-3)

我已经尝试了这个并且它完成了工作

SELECT
    c.col1, c.col3, m.col3, m.col4,
    count(m.col1), count(re.col2) AS cnt,
    GROUP_CONCAT(p.col1 ORDER BY p.col1) AS "Group1",
    GROUP_CONCAT(p.col2 ORDER BY p.col1) AS "Group2", 
    GROUP_CONCAT(CASE WHEN con.col3 is null THEN p.col1 ELSE con.col3 END),
    con.col4, con.col5, p.col3
FROM t1 re
    INNER JOIN t2 c   ON (re.col1  = c.col1)
    INNER JOIN (
        SELECT col1, col2, col3, col4, col5 FROM t3 ORDER BY col1
    ) AS          p   ON (re.col2  = p.col1)
    LEFT JOIN  t4 con ON (con.col1 = p.col2)
    INNER JOIN t5 m   ON (m.col1   = c.col5) 
GROUP BY re.col1