2014-04-15 37 views
2

我需要顯示每個「ID」有多少個不同的值。SQL - CASE當計數不同的值

它應該是這樣的:

id | component_a | component_b | component_c 
-------------------------------------------------- 
KLS11 |  none  |  one |  none  
KLS12 |  one  |  one |  none   
KLS13 |  several |  one |  none   
KLS14 |  one  |  one |  one    
KLS15 |  one  | several |  several   

我有如下表(表-A):

id | component_a | component_b | component_c 
-------------------------------------------------- 
KLS11 |    |  a  |    
KLS12 |  a  |  a  |    
KLS13 |  a  |  a  |    
KLS13 |  b  |  a  |    
KLS14 |  a  |  a  |  a   
KLS15 |  a  |  a  |  a     
KLS15 |  a  |  b  |  b 

下面的例子/解釋:

  • KLS13有不同的價值觀component_a(a,b) - 所以它應該顯示'幾個'
  • KLS13具有component_b(A,A)相同的值 - 因此它應該顯示 '一個'
  • KLS13具有component_c沒有價值 - 因此它應該顯示 ''

這是我的SQL代碼:

我已經爲component_a做了它,但它不起作用。我究竟做錯了什麼?

SELECT 
CASE WHEN component_a is NULL THEN 'none' 
    WHEN (SELECT count(DISTINCT component_a) 
      FROM table_a 
       WHERE id=(SELECT id 
          FROM table_a GROUP BY id HAVING count(*)>1)>1 THEN 'several' 
    WHEN (SELECT count(DISTINCT component_a) 
      FROM table_a 
       WHERE id=(SELECT id 
          FROM table_a GROUP BY id HAVING count(*)>1)=1 THEN 'one' 
END as componentA 
FROM table_a 

我是一名SQL初學者,所以我將不勝感激任何幫助。

有一個愉快的一天

+0

我想你以前END.Also沒有工作需要ELSE沒有太大的幫助。 – Mihai

+0

「不起作用」是什麼意思?你得到一個錯誤,或只是不是你想要的結果? –

+0

你可以使用[SQLFiddle](http://sqlfiddle.com/)嗎? – Alexander

回答

6

你得到一個ORA-00936錯誤(我認爲),因爲你是不是每個when分支內關閉括號;因爲子子選擇(與having子句一起)返回多個行,所以沒有相關性,因此添加額外的關閉會將錯誤更改爲'ORA-01427:單行子查詢返回多個行'。

你不需要的子查詢,你只需要統計不同的值作爲case結構的一部分,建立一個searched case expression

select id, 
    case count(distinct component_a) 
    when 0 then 'none' 
    when 1 then 'one' 
    else 'several' 
    end as component_a 
from table_a 
group by id 
order by id; 

ID COMPONENT_A 
----- ----------- 
KLS11 none   
KLS12 one   
KLS13 several  
KLS14 one   
KLS15 one   

再次爲其他列:

select id, 
    case count(distinct component_a) 
    when 0 then 'none' 
    when 1 then 'one' 
    else 'several' 
    end as component_a, 
    case count(distinct component_b) 
    when 0 then 'none' 
    when 1 then 'one' 
    else 'several' 
    end as component_b, 
    case count(distinct component_c) 
    when 0 then 'none' 
    when 1 then 'one' 
    else 'several' 
    end as component_c 
from table_a 
group by id 
order by id; 

ID COMPONENT_A COMPONENT_B COMPONENT_C 
----- ----------- ----------- ----------- 
KLS11 none  one   none   
KLS12 one   one   none   
KLS13 several  one   none   
KLS14 one   one   one   
KLS15 one   several  several  
+0

+1)非常好的答案。 – Hamidreza

0

嘗試此查詢:

WITH t1 as 
(SELECT COUNT(DISTINCT COMPONENT_A) COMPONENT_A, 
COUNT(DISTINCT COMPONENT_B) COMPONENT_B,COUNT(DISTINCT COMPONENT_C) COMPONENT_C 
FROM TABLE1 GROUP BY ID) 
SELECT 
CASE 
WHEN COMPONENT_A = 1 THEN 'one' 
WHEN COMPONENT_A > 1 THEN 'several' ELSE 'none' END AS COMPONENT_A, 
CASE 
WHEN COMPONENT_B = 1 THEN 'one' 
WHEN COMPONENT_B > 1 THEN 'several' ELSE 'none' END AS COMPONENT_B, 
CASE 
WHEN COMPONENT_C = 1 THEN 'one' 
WHEN COMPONENT_C > 1 THEN 'several' ELSE 'none' END AS COMPONENT_C 
FROM t1; 
+0

只適用於此特定數據;如果添加一個「c」值,那麼它就根本不算,如果一個ID只有「b」,那麼它將它報告爲「多個」而不是「一個」,例如。在某個地方必須有一個「計數」。 –

+0

是的你是對的我沒有讀過所有的問題,謝謝你的評論。 – Hamidreza

+0

我現在編輯我的答案,它工作正常。 – Hamidreza