title: Using ORDER BY clause inside GROUP_CONCAT function in SQLite
body: <p>I don't think that I can use <code>ORDER BY</code> clause  inside the <code>GROUP_CONCAT</code> function. </p>
  
  <p>Does anyone know a tricky way to accomplish this behavior in <code>SQLite</code>?</p>
  
  <p>I saw this <a href="https://stackoverflow.com/questions/1897352/sqlite-group-concat-ordering">question</a> before. But I have a complex query .</p>
  
  <p>My statement looks like this:</p>
  
  <pre><code>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 
  </code></pre>
  
  <p><code>Group1</code> and <code>Group2</code> is coming from the same table but different columns: I want to preserve the order of the <code>Group1</code> with <code>Group2</code>:</p>
  
  <pre><code>table t3 
  +------+------+
  | col1 | col2 |
  +------+------+
  |    1 | A    |
  |    2 | B    |
  |    3 | C    |
  |    4 | D    |
  |    5 | E    |
  +------+------+
  </code></pre>
  
  <p>so if <code>Group1</code> appears like this <code>2,1,3</code> <code>Group2</code> should appear like this <code>B,A,C</code></p>