日期:2014-05-17  浏览次数:20757 次

求一SQL语句!!
有表如下:

流水号 ID 数量 类型
1001 A1 20 X
1002 A1 10 X
1003 A2 40 X 
1004 A2 20 Y
1005 A3 80 Z
1006 A3 20 Y
1007 A3 10 Y

求一SQL语句欲得到如下结果:

A1 30 X
A2 60 X,Y
A3 110 Y,Z

------解决方案--------------------
SQL code

with a as(
select 1001 id,'A1' type,20 value,'X' flag from dual UNION all
select 1002,'A1',10,'X' from dual union all
select 1003,'A2',40,'X' from dual union all 
select 1004,'A2',20,'Y' from dual union all
select 1005,'A3',80,'Z' from dual union all
select 1006,'A3',20,'Y' from dual union all
select 1007,'A3',10,'Y' from dual) 
SELECT TYPE,SUM(VALUE),wm_concat(DISTINCT flag) FROM a GROUP BY TYPE;