简体   繁体   English

SQL代码中的重复值

[英]duplicated values in SQL code

I have the below SQL code - 我有以下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))

and I expect to receive the value - 1080.104079 我希望收到该值-1080.104079

在此处输入图片说明

However, it gives me two values - 但是,它给了我两个价值-

在此处输入图片说明

This value 1079.459992 is get, because the exr has been swept, which is wrong, as the charge for 'Feb' should be multiplied on exr for 'Feb' 获得此值1079.459992,因为exr已被清除,这是错误的,因为应将“ Feb”的费用乘以exr中的“ Feb”

在此处输入图片说明

Could you please help me to correct the code, so that I can receive only true value, which is 1080.104079. 您能否帮助我更正代码,以便我只能接收真实值1080.104079。

If all you need is one result, you could try moving the CASE logic into the SUM function itself: 如果只需要一个结果,则可以尝试将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';

Part of the problem seems to be that you're grouping by multiple columns, yet selecting only one or two. 问题的一部分似乎是您要按多列分组,但只能选择一两个。 If you group by the opr column only, you should only get one row for 'DOM'. 如果仅按opr列分组,则“ DOM”仅应获得一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM