在oracle中使用SUM函数

时间:2015-11-18 10:06:48

标签: sql oracle sum analytics

我在Oracle中有一个包含以下内容的表:

 id | month | payment | rev
----------------------------
  A |  1    |  10     |  0
  A |  2    |  20     |  0
  A |  2    |  30     |  1
  A |  3    |  40     |  0
  A |  4    |  50     |  0
  A |  4    |  60     |  1
  A |  4    |  70     |  2

我想计算付款栏(SUM(payment))。对于(id=A month=2)(id=A month=4),我只想从REV列中获取最大值。所以总和是(10+30+40+70)=150。怎么做?

4 个答案:

答案 0 :(得分:2)

您也可以在下方使用。

select id,sum(payment) as value
    from 
    (
    select id,month,max(payment) from table1
    group by id,month
    )
    group by id

Edit:用于检查最大转速值

select id,sum(payment) as value
from (
select id,month,rev,payment ,row_number() over (partition by id,month order by rev desc) as rno     from table1
) where rno=1
group by id

答案 1 :(得分:1)

这预先假定您每转不超过一个值。如果情况并非如此,那么您可能需要var timer, fullText, currentOffset, onComplete; function Speak(person, text, callback) { $("#name").html(person); fullText = text; currentOffset = 0; onComplete = callback; timer = setInterval(onTick, 100); } function onTick() { currentOffset++; if (currentOffset == fullText.length) { complete(); return; } var text = fullText.substring(0, currentOffset); $("#message").html(text); } function complete() { clearInterval(timer); timer = null; $("#message").html(fullText); onComplete(); } $(".box").click(function () { complete(); }); Speak("Simon", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", function () { setTimeout(function(){ Speak("Javascript", "Simon has finished speaking!"); }, 2000); }); 分析而不是row_number

max

答案 2 :(得分:0)

从tableName中选择sum(payment),其中id ='A',month = 2 OR month = 4 order by payment asc;

答案 3 :(得分:0)

或者就是这样,如果我理解了正确的要求:

with demo as (
    select 'A'as id, 1 as month, 10 as payment, 0 as rev from dual
    union all select 'A',2,20,0 from dual
    union all select 'A',2,30,1 from dual
    union all select 'A',3,40,0 from dual
    union all select 'A',4,50,0 from dual
    union all select 'A',4,60,1 from dual
    union all select 'A',4,70,2 from dual
)
select sum(payment) keep (dense_rank last order by rev)
from   demo;

您可以通过添加关键列来检查细分:

with demo as (
    select 'A'as id, 1 as month, 10 as payment, 0 as rev from dual
    union all select 'A',2,20,0 from dual
    union all select 'A',2,30,1 from dual
    union all select 'A',3,40,0 from dual
    union all select 'A',4,50,0 from dual
    union all select 'A',4,60,1 from dual
    union all select 'A',4,70,2 from dual
)
select id, month, max(rev)
     , sum(payment) keep (dense_rank last order by rev)
from   demo
group by id, month;