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

高分求SQL!谢谢!
SQL code

tradeid demin_class_id totalvalue
1        1               10
1        2               10
1        3               10
2        2               20
2        3               20
2        5               20
2        6               20

需求是这样的:假设tradeid为报销单号,求出所有报销费用之和,demin_class_id表示同一张报销单的不同分类,即一张报销单
按不同的分类,其费用是一致的,现在要计算所有报销费用之和!谢谢!在线等!



------解决方案--------------------
SQL code
select tradeid,sum(totalvalue) from tablename group by tradeid

------解决方案--------------------
SQL code
select tradeid ,sum(totalvalue)
   from(
   select distincts 
tradeid , totalvalue
from a

    )
group by tradeid

------解决方案--------------------
select sum(totalvalue) from test order by tradeid,demin_class_id group by tradeid,demin_class_id;


------解决方案--------------------
不是很明白楼主意思。按报销单求费用和的话就直接
select sum(totalvalue) from tablename group by tradeid;
求总和的话就
select sum(totalvalue) from tablename;
不会问的这么简单吧。如果不是再说明白点。
------解决方案--------------------
是每个报销单号的所有费用.还是所有有费用?
每个:
select tradeid,sum(totalvalue) from 表名 group by tradeid;
所有:
select sum(totalvalue) from 表名;

------解决方案--------------------
select demin_class_id,sum(totalvalue) from test order by demin_class_id group by demin_class_id; 


------解决方案--------------------
引用楼主 Fenglee2008 的帖子:
SQL code
tradeid demin_class_id totalvalue
1 1 10
1 2 10
1 3 10
2 2 20
2 3 20
2 5 20
2 6 20

需求是这样的:假设tradeid为报销单号,求出所有报销费用之和,demin_class_id表示同一张报销单的不同分类,即一张报销单
按不同的分类,其费用是一致的,现在…

------解决方案--------------------
是不是这个答案呢
WITH a AS (SELECT '1' ID ,1 cid ,10 sal FROM dual
UNION ALL
SELECT '1' ID ,2 cid ,10 sal FROM dual
UNION ALL
SELECT '1' ID ,3 cid ,10 sal FROM dual
UNION ALL
SELECT '2' ID ,2 cid ,20 sal FROM dual
UNION ALL
SELECT '2' ID ,3 cid ,20 sal FROM dual
UNION ALL
SELECT '2' ID ,5 cid ,20 sal FROM dual
UNION ALL
SELECT '2' ID ,6 cid ,20 sal FROM dual
)
SELECT 'id-'||nvl(ID,'总和') ,SUM(sal) FROM a 
group by rollup(ID)
UNION ALL
SELECT 'cid-'||nvl(''||cid,'总和') ,SUM(sal) FROM a 
group by rollup(cid);

结果是:

id-1 30
id-2 80
id-总和 110
cid-1 10
cid-2 30
cid-3 30
cid-5 20
cid-6 20
cid-总和 110
------解决方案--------------------
select tradeid ,sum(totalvalue)
from(
select distinct 
tradeid , totalvalue
from a

)
group by tradeid
------解决方案--------------------


 select sum(totalvalue) as 总费用
from( 
select distinct 
tradeid , totalvalue 
from a) 

 
------解决方案--------------------
select sum(totalvalue) from tbl
------解决方案--------------------