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

请问种如何写SELECT,谢谢
表A   字段   中有   订单类型PART_ORDER_TYPE,订单金额PART_ORDER_AMOUNT
          根据PART_ORDER_TYPE(有4种类型)分别计算出各种类型对应PART_ORDER_AMOUNT的和,以及每种类型PART_ORDER_AMOUNT和占总订单金额的百分比
请问种如何写SELECT,谢谢


------解决方案--------------------
select PART_ORDER_TYPE,sum(PART_ORDER_AMOUNT) "总和 ",
sum(PART_ORDER_AMOUNT)/(select sum(PART_ORDER_AMOUNT) from a) "百分比 "
from a group by PART_ORDER_TYPE;
------解决方案--------------------
命令没有错,可能是四舍五入的问题
------解决方案--------------------
select tt.PART_ORDER_TYPE,
sum(tt.PART_ORDER_AMOUNT) TOTAL_AMOUNT,
round(RATIO_TO_REPORT(sum(tt.PART_ORDER_AMOUNT)) over() * 100, 2) || '% ' as percent
from A tt
group by tt.PART_ORDER_TYPE;

------解决方案--------------------
select
aa.part_order_type
,aa.part_total
,case when bb.total = 0 then 0 else aa.part_total / bb.total end
from (
select a.part_order_type, sum(a.part_order_amount) part_total
from a
group by a.part_order_type
) aa
left join (select sum(a.part_order_amount) total from a) bb
on 1 = 1