我的表是:
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
答案 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;