如何仅计算char数据类型列中的缺失/空白值

时间:2015-11-02 10:31:27

标签: mysql sql

我的表是:

date       value       name
may02       22          s1
may03                   s1
may04       34          s2
may05       35          s2
may06                   s3
may07                   s3
may08       22          s3

值是char数据类型。我的问题是

select count(*) as missing_value from my_table  where value= “ “ group by name;

以上查询无法正常运行 我需要在按名称

分组的值列中仅计算MISSING / BLANK计数

O / P应该是

name       missing_value
s1           1
s2           0
s3           2

1 个答案:

答案 0 :(得分:2)

您可以使用CASE WHEN

SELECT name,
  COUNT(CASE WHEN `value` IS NULL OR TRIM(`value`) = '' THEN 1 ELSE NULL END) 
  AS missing_value 
FROM my_table 
GROUP BY name;

SqlFiddleDemo

或(这将跳过不具有任何空/空数据的组):

SELECT name,
  COUNT(*) as missing_value 
FROM my_table 
WHERE `value` IS NULL OR TRIM(`value`) = ''
GROUP BY name;