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

oracle的一个分组查询问题
table:
单据号 姓名 金额
A name1 100
A name1 200
A name2 300
B name1 400
B name2 500
要求同一个单据号的按照姓名合起来


查询后结果:

单据号 姓名 金额
A name1 300(有100+200得到)
A name2 300
B name1 300
B name2 500

------解决方案--------------------
SQL code
select 单据号 ,姓名,sum(金额)
from table
group by 单据号,姓名

------解决方案--------------------
with tab as (
select 'A' as djh,'name1' as name,100 as je from dual
union all
select 'A' as djh,'name1' as name,200 as je from dual
union all
select 'A' as djh,'name2' as name,300 as je from dual
union all
select 'B' as djh,'name1' as name,400 as je from dual
union all
select 'B' as djh,'name2' as name,500 as je from dual
)
select djh,name,sum(je) as je from tab group by djh,name
------解决方案--------------------
SQL code

--这个蛮基础的啊 楼主没认真去想吧
select 单据号 ,
       姓名,
       sum(金额)  总金额
from tb
group by 单据号,姓名

------解决方案--------------------
探讨
SQL code

--这个蛮基础的啊 楼主没认真去想吧
select 单据号 ,
姓名,
sum(金额) 总金额
from tb
group by 单据号,姓名

------解决方案--------------------
SQL code
select 
单据号 , 姓名, sum(金额)  总金额
from tb
group by 单据号,姓名

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

SQL> with t as (
  2       select 'A' 单据号,'name1' 姓名,100 金额 from dual union all
  3       select 'A','name1',200 from dual union all
  4       select 'A','name2',300 from dual union all
  5       select 'B','name1',400 from dual union all
  6       select 'B','name2',500 from dual)
  7  select 单据号,姓名,sum(金额)
  8  from t
  9  group by 单据号,姓名;

单据号 姓名   SUM(金额)
------ ----- ----------
A      name1        300
A      name2        300
B      name1        400
B      name2        500