SQL - CASE WHEN计算不同的值

Jen*_*O88 3 sql oracle count having case-when

我需要显示每个'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           
Run Code Online (Sandbox Code Playgroud)

我有下表(table_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
Run Code Online (Sandbox Code Playgroud)

这是一个例子/解释:

  • KLS13在component_a(a,b)中有不同的值 - 所以它应该显示' 几个 '
  • KLS13在component_b(a,a)中具有相同的值 - 所以它应该显示' one '
  • KLS13在component_c中没有任何价值 - 所以它应该显示' none '

这是我的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
Run Code Online (Sandbox Code Playgroud)

我是SQL的初学者,所以我将不胜感激任何帮助.

祝你今天愉快

Ale*_*ole 7

您收到ORA-00936错误(我认为),因为您没有关闭每个when分支中的括号; 添加额外的关闭会将错误更改为'ORA-01427:单行子查询返回多行',因为子子选择(带有having子句)返回多行 - 没有相关性.

您不需要子查询,只需将不同的值计算为case构造的一部分,以创建搜索的案例表达式:

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         
Run Code Online (Sandbox Code Playgroud)

并重复其他列:

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     
Run Code Online (Sandbox Code Playgroud)