0

我有以下脚本 -

select sum(duration) as duration from opr 
       where op in ('GRC','GCQ')
       and timestamp between '20130101000000' and '20130930235959'

我收到值 - 34459298秒。我想包括这些限制 - 持续时间是

  • <= '18000000'(秒)应乘以 0.14

  • > '18000000' 和 <= '27000000' 之间的持续时间应乘以 0.11

  • 并且持续时间 > '27000000' 应该乘以 0.09

我已经尝试过这个案例陈述 -

   case when duration <= '18000000' 
        then (duration)*0.14
        when duration > '18000000' and duration <= '27000000'
        then (duration)*0.11
        when duration > '27000000' 
        then (duration)*0.09
          else 0 end as bal

但是,我收到这个值34459298乘以 0.09,因为它大于“27000000”,这不是想法。这个想法是这两个操作('​​GRC','GCQ')乘以上述值的所有秒数。

你能帮我做这件事吗?

4

4 回答 4

3

duration数字还是字符串?我猜想与数字进行比较会更好:

SELECT
    SUM(
      CASE
        WHEN duration <= 18000000 THEN duration * 0.14
        WHEN duration > 18000000 AND duration <= 27000000 THEN duration * 0.11
        WHEN duration > 27000000 THEN duration * 0.09
        ELSE 0
      END
    ) AS duration
  FROM opr 
WHERE
  op IN ('GRC','GCQ')
  AND timestamp BETWEEN '20130101000000' AND '20130930235959'
;
于 2013-10-28T10:19:35.910 回答
1

使用子查询选择特定的持续时间并将持续时间乘以您的值,而不是对该子查询使用 sum。

于 2013-10-28T10:17:08.860 回答
1

尝试这个:

select sum(case
        when duration <= '18000000' then (duration)*0.14
        when duration > '18000000' and duration <= '27000000' then (duration)*0.11
        when duration > '27000000' then (duration)*0.09
        else 0
    end) as bal
from opr 
where op in ('GRC','GCQ')
    and timestamp between '20130101000000' and '20130930235959'
于 2013-10-28T10:18:11.093 回答
1

我想你想这样做

select sum(case when duration <= '18000000' then (duration)*0.14
                when duration > '18000000' and duration <= '27000000' then (duration)*0.11
                when duration > '27000000' then (duration)*0.09
                else 0 end as bal) as duration 
from opr 
where op in ('GRC','GCQ')
and timestamp between '20130101000000' and '20130930235959'
于 2013-10-28T10:18:37.783 回答