0

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

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

此外,我在 01/01/2013 - 01/03/2013 期间使用数字0.12,但是我想在 01/03/2013 之后的期间使用不同的数字(例如0.10)。我的一个时间段的列被命名为时间戳(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) 显示在另一列中。

在此处输入图像描述

4

1 回答 1

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;
于 2013-10-18T07:01:17.183 回答