2014-02-07 72 views
0

我有一個MySQL查詢爲:產量並不如預期

select count(srcount) as SR_Count, 
CASE assignee_name 
when 'U80040300' then 'ABC' 
when 'U80036396' then 'DEF' 
when 'U80035831' then 'GHI' 
when 'U80042986' then 'JKL' 
else 'A' 
end as Guys_Name 
from data 
GROUP BY assignee_name; 

其中給出的

SR_COUNT GUYS_NAME 
3   A 
5   A 
3   GHI 
2   DEF 
3   ABC 
2   JKL 

輸出,但我想要的輸出

SR_COUNT GUYS_NAME 
    8   A 
    3   GHI 
    2   DEF 
    3   ABC 
    2   JKL 

這裏是SQLFiddle

任何幫助,將不勝感激。 在此先感謝..

回答

1
select count(srcount) as SR_Count, guys_name 
from 
(select srcount, 
CASE assignee_name 
when 'U80040300' then 'ABC' 
when 'U80036396' then 'DEF' 
when 'U80035831' then 'GHI' 
when 'U80042986' then 'JKL' 
else 'A' 
end as Guys_Name 
from data) as mydata 
GROUP BY guys_name; 

只是分組需要在你的情況的結果。

2

集團的結果通過Guys_Name

select count(srcount) as SR_Count, 
CASE assignee_name 
when 'U80040300' then 'ABC' 
when 'U80036396' then 'DEF' 
when 'U80035831' then 'GHI' 
when 'U80042986' then 'JKL' 
else 'A' 
end as Guys_Name 
from data 
GROUP BY Guys_Name; 
+0

但由於Guys_name是別名,它不支持在'Oracle SQL開發者'中。 –

+0

Oracle不支持直接使用別名進行分組。它必須首先被抄襲,正如我在我的回答中所提到的那樣。 –

+0

我錯過了有關使用Oracle數據庫的信息。所以看看@OracleUser的答案。 – Daniel