SQL语句中的多个sum

时间:2013-08-06 12:52:31

标签: sql oracle

我与PL / SQL开发人员合作。在这个SQL语句后面我有SQL视图。

从下面的代码中,我收取 FINE - 87 FINR - 65 的费用。我希望在另一列中总结 FINE FINR 的结果。

此外,我在01/01/2013 - 01/03/2013期间使用数字 0.12 ,但是我想在2013年1月3日之后使用不同的数字(例如的 0.10 )。我的一段时间段名为timestamp(varchar)。

你知道我怎么做这些事吗?

select name_table_name, 
-- FINE, FINR 
case      
    when name_table_name in ('FINE','FINR') 
    and table_seconds <= table_balanced
    then (table_sd * 1.149798) - ((table_seconds/60)0.12)                          
    when name_table_name in ('FINE','FINR') 
        and table_seconds > table_balanced
    then (table_sd * 1.149798) - ((table_seconds/60)0.12)
    -- RUSN     
    when name_table_name = 'RUSN' 
    then (table_sd * 1.149798)-((table_seconds/60)*0.20)
else 0 end as charge

这是从数据库截取的屏幕截图。我希望FINE和FINR的总和(156.66)显示在另一列中。

enter image description here

1 个答案:

答案 0 :(得分:1)

select 
case
  when name_table_name in ('FINE','FINR')
  then 'FINE or FINR'
  when name_table_name = 'RUSN'
  then 'RUSN'
  else 'others'
end as name_table_name, 
sum (
case      
    when name_table_name in ('FINE','FINR') 
    and table_seconds <= table_balanced
    then (table_sd * 1.149798) - ((table_seconds/60)*0.12)                          
    when name_table_name in ('FINE','FINR') 
        and table_seconds > table_balanced
    then (table_sd * 1.149798) - ((table_seconds/60)*0.12)
    when name_table_name = 'RUSN' 
    then (table_sd * 1.149798)-((table_seconds/60)*0.20)
else 0 end) as charge
from table
group by 
case
  when name_table_name in ('FINE','FINR')
  then 'FINE or FINR'
  when name_table_name = 'RUSN'
  then 'RUSN'
  else 'others'
end;