SQL代码中的重复值

时间:2013-10-07 13:23:09

标签: sql database oracle

我有以下SQL代码 -

 select distinct opr,  
  CASE
          WHEN timestamp like '201302%' THEN ((SUM (charge / POWER (10, decimals)) OVER (PARTITION BY opr) * 1.15306))
          WHEN timestamp like '201303%' THEN ((SUM (charge / POWER (10, decimals)) OVER (PARTITION BY opr)* 1.14979))
          ELSE 0
       END as exr
  FROM in
group by opr, timestamp, (charge / POWER (10, decimals))

我希望收到价值 - 1080.104079

enter image description here

但是,它给了我两个值 -

enter image description here

此值为1079.459992,因为exr已被扫描,这是错误的,因为'Feb'的费用应该乘以'2月'的exr

enter image description here

请你帮我纠正一下代码,这样我才能收到真正的价值,即1080.104079。

1 个答案:

答案 0 :(得分:1)

如果只需要一个结果,您可以尝试将CASE逻辑移入SUM函数本身:

select opr,  
    SUM(CASE
          WHEN timestamp like '201302%' THEN ((charge / POWER (10, decimals)) * 1.15306)
          WHEN timestamp like '201303%' THEN ((charge / POWER (10, decimals)) * 1.14979)
          ELSE 0
       END)
       as exr
  FROM in
group by opr
having opr = 'DOM';

问题的一部分似乎是您按多列分组,但只选择一个或两个。如果仅按opr列进行分组,则只应为“DOM”获取一行。