select table.id,sum(A),sum(B) from table group by table.id
要是3有三条是怎么合并? 3 2 NULL 3 NULL 3 3 3 3
试试看~~~SQL> select tt.ID,max(tt.A),max(tt.B) 2 from ( 3 select 1 as ID,NULL as A,4 as B from dual 4 union all 5 select 2 as ID,5 as A,NULL as B from dual 6 union all 7 select 3 as ID,2 as A,NULL as B from dual 8 union all 9 select 3 as ID,NULL as A,3 as B from dual 10 )tt 11 group by tt.ID 12 order by tt.ID;==================result==================== ID MAX(TT.A) MAX(TT.B) ---------- ---------- ---------- 1 4 2 5 3 2 3
3 2 NULL
3 NULL 3
3 3 3
2 from (
3 select 1 as ID,NULL as A,4 as B from dual
4 union all
5 select 2 as ID,5 as A,NULL as B from dual
6 union all
7 select 3 as ID,2 as A,NULL as B from dual
8 union all
9 select 3 as ID,NULL as A,3 as B from dual
10 )tt
11 group by tt.ID
12 order by tt.ID;==================result==================== ID MAX(TT.A) MAX(TT.B)
---------- ---------- ----------
1 4
2 5
3 2 3