【问题标题】:SQL - CASE WHEN count different valuesSQL - CASE WHEN 计算不同的值
【发布时间】:2014-05-29 12:11:48
【问题描述】:

我需要显示每个 '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           

我有下表(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

这里是一个例子/解释:

  • KLS13 在 component_a (a,b) 中有不同的值 - 所以它应该显示 'several'
  • KLS13 在 component_b ( a,a ) 中具有相同的值 - 所以它应该显示 '一个'
  • 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

我是 SQL 的初学者,所以我将不胜感激。

祝你有美好的一天

【问题讨论】:

  • 我认为您在 END 之前需要 ELSE。也没有什么帮助。
  • “没用”是什么意思?你得到一个错误,还是不是你想要的结果?
  • 你能用SQLFiddle吗?

标签: sql oracle count having case-when


【解决方案1】:

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

您不需要子查询,只需将不同的值计算为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     

【讨论】:

    【解决方案2】:

    试试这个查询:

    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;
    

    【讨论】:

    • 这只适用于这个特定的数据;例如,如果添加 'c' 的值,则根本不计算在内,如果 ID 只有 b,则将其报告为 several 而不是 one。某处必须有count
    • 是的,你是对的,我没有阅读所有问题,感谢您的评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多