2016-05-13 85 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

相關問題