2016-05-13 88 views
0

我试图让特定列的总和Oracle查询得到列的和不同的价值观

我想Oracle查询得到的列“timespend”之和为(S_1,S_4每个场景,S_5,S_8)其中名称是等于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 

我已经使用下面的查询,但其用柱timespend的总和返回

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 

谁能帮助我获得预期的输出像上面提到的?

+0

您可以发布您的查询输出和您需要的输出吗? – Aleksej

+1

你想要的时间总和,你得到的时间总和。我现在很困惑。 –

+0

我已更新我的查询。我想timespend为组场景,它可以查找名称B.类似的总和,但我m到处总和 – surendar

回答

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); 
+0

我得到了下面的查询结果符合预期。谢谢从表1中选择总和(timespend),场景名称=“B”和场景(“S_1”,“S-3”,“S_4”,“S_5”,“S_8”) 组由情景 为了通过情景 – surendar

+0

@surendar:这似乎是你在你的问题中发布的相同查询;不是吗? – Aleksej

+0

@Aleksej看起来对我来说!显然,在OP的数据库中有gremlins或其他东西! * {;-) – Boneist

相关问题