Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确定SQL表a列中不同值在B列中出现空值的次数_Sql_Sql Server - Fatal编程技术网

确定SQL表a列中不同值在B列中出现空值的次数

确定SQL表a列中不同值在B列中出现空值的次数,sql,sql-server,Sql,Sql Server,我有一个SQL表,其中“name”作为一列,date作为另一列,location作为第三列。位置列支持空值 我正在尝试编写一个查询,以确定名称列中每个不同值在location列中出现null值的次数 有人能帮忙吗 一种方法使用条件聚合: select name, sum(case when location is null then 1 else 0 end) from t group by name; 另一种稍微少打字的方法是: select name, count(*) - count(l

我有一个SQL表,其中“name”作为一列,date作为另一列,location作为第三列。位置列支持空值

我正在尝试编写一个查询,以确定名称列中每个不同值在location列中出现null值的次数


有人能帮忙吗

一种方法使用条件聚合:

select name, sum(case when location is null then 1 else 0 end)
from t
group by name;
另一种稍微少打字的方法是:

select name, count(*) - count(location)
from t
group by name;

将count与筛选器一起使用,因为您只需要出现Null

select name, count(*) occurances
from mytable 
where location is null
group by name

从您的问题中,您需要获得所有不同“名称”行的不同列表,然后您需要计算每个名称有多少个空值

以下将实现这一目标:

SELECT name, count(*) as null_counts
FROM table 
WHERE location IS NULL
GROUP BY name
WHERE子句将仅检索记录位置为NULL的记录

分组依据将基于名称透视数据


SELECT将为您提供每个名称的名称和记录数(*)。

到目前为止您尝试了什么以及任何样本日期?虽然此代码可能会回答此问题,但提供有关如何和/或为什么解决此问题的其他上下文将提高答案的长期价值。