首页 > 代码库 > mysql group by后 拼接某一字段
mysql group by后 拼接某一字段
SQL> select * from tmp10;
JS ND JM
--------- ----- --------------------------------------------------
10 2006 DAQING01
20 2007 DAQIN03
30 2006 SHENGLI01
40 2007 KELAMAYI01
SQL>
SQL> SELECT ND,
2 sum(JS) sum_js,
3 MAX(LTRIM(SYS_CONNECT_BY_PATH(JM, ‘,‘), ‘,‘)) sum_jm
4 FROM (SELECT ND,
5 JS,
6 JM,
7 ROW_NUMBER() OVER(PARTITION BY ND ORDER BY JS) RN
8 FROM tmp10)
9 START WITH RN = 1
10 CONNECT BY PRIOR RN + 1 = RN AND PRIOR ND = ND
11 GROUP BY ND;
ND SUM_JS SUM_JM
----- ---------- --------------------------------------------------------------------------------
2006 40 DAQING01,SHENGLI01
2007 60 DAQIN03,KELAMAYI01
SQL>
来自:http://www.itpub.net/thread-884474-1-1.html
后来才发现mysql中有GROUP_CONCAT()这个函数。。。
mysql group by后 拼接某一字段