oracle查询获取不同值的列总和

时间:2016-05-13 10:05:30

标签: sql oracle plsqldeveloper

我正在尝试获取特定列的总和

我希望oracle查询获取(S_1,S_4,S_5,S_8)中每个方案的列“timespend”的总和,其中name等于B

name id scenario timespend
A   123  S_1     2
A   123  S_1     5
A   123  S_3    6.3
B   124  S_1    3
B   124  S_1    8.9
B   124  S_1    5
B   124  S_1    4
B   124  S_5    1.23
B   124  S_5    56
B   124  S_5    8
B   124  S_8    9
B   124  S_8    4
C   125  S_8    6
D   126  S_2    9
D   126  s_4    5
D   126  s_4    6.2
D   126  s_4    7
E   127  S_1    8
E   127  S_1    1

我使用了以下查询,但是它以列timepend的总和返回

select sum(timespend),
       scenario
from   table1
where  scenario in ('s_1','s_3','s_4','s_5','s_8')
and    name = 'B'
group by scenario

查询的输出是

sum(timespend) scenario
-------------- --------
154.63          s_1
154.63          s_5
154.63          s_8

预期输出

sum(timespend) scenario
-------------- --------
20.9            s_1
65.23           s_5
19              s_8

任何人都可以帮助我获得如上所述的预期输出吗?

1 个答案:

答案 0 :(得分:2)

我没有看到您的查询出现问题(尽管您对场景S_8的预期输出是错误的,另外您在where子句中使用了小写s):

with table1 as (select 'A' name, 123 id, 'S_1' scenario, 2 timespend from dual union all
                select 'A' name, 123 id, 'S_1' scenario, 5 timespend from dual union all
                select 'A' name, 123 id, 'S_3' scenario, 6.3 timespend from dual union all
                select 'B' name, 124 id, 'S_1' scenario, 3 timespend from dual union all
                select 'B' name, 124 id, 'S_1' scenario, 8.9 timespend from dual union all
                select 'B' name, 124 id, 'S_1' scenario, 5 timespend from dual union all
                select 'B' name, 124 id, 'S_1' scenario, 4 timespend from dual union all
                select 'B' name, 124 id, 'S_5' scenario, 1.23 timespend from dual union all
                select 'B' name, 124 id, 'S_5' scenario, 56 timespend from dual union all
                select 'B' name, 124 id, 'S_5' scenario, 8 timespend from dual union all
                select 'B' name, 124 id, 'S_8' scenario, 9 timespend from dual union all
                select 'B' name, 124 id, 'S_8' scenario, 4 timespend from dual union all
                select 'C' name, 125 id, 'S_8' scenario, 6 timespend from dual union all
                select 'D' name, 126 id, 'S_2' scenario, 9 timespend from dual union all
                select 'D' name, 126 id, 'S_4' scenario, 5 timespend from dual union all
                select 'D' name, 126 id, 'S_4' scenario, 6.2 timespend from dual union all
                select 'D' name, 126 id, 'S_4' scenario, 7 timespend from dual union all
                select 'E' name, 127 id, 'S_1' scenario, 8 timespend from dual union all
                select 'E' name, 127 id, 'S_1' scenario, 1 from dual)
-- end of mimicking your table1 with data in it. See SQL below:
select sum(timespend),
       scenario
from   table1
where  scenario in ('S_1','S_3','S_4','S_5','S_8')
and    name = 'B'
group by scenario
order by scenario;

SUM(TIMESPEND) SCENARIO
-------------- --------
          20.9 S_1     
         65.23 S_5     
            13 S_8  

或许,从您的示例数据中的某些方案数据是小写的并且您提到您正在寻找set of scenarios that look similar这一事实来判断,您可能需要以下内容?

select sum(timespend),
       upper(scenario) scenario
from   table1
where  upper(scenario) in ('S_1','S_3','S_4','S_5','S_8')
and    name = 'B'
group by upper(scenario)
order by upper(scenario);