计数单个查询中的空值和非空值
我有一张桌子
create table us ( a number );
现在我有像这样的数据:
a 1 2 3 4 null null null 8 9
现在我需要一个查询来计算列a中的空值和非空值
这适用于Oracle和SQL Server(您可能可以使其在另一个RDBMS上工作):
select sum(case when a is null then 1 else 0 end) count_nulls , count(a) count_not_nulls from us;
要么:
select count(*) - count(a), count(a) from us;
如果我理解正确,你想要计数列中的所有NULL和所有NOT NULL …
如果这是正确的:
SELECT count(*) FROM us WHERE a IS NULL UNION ALL SELECT count(*) FROM us WHERE a IS NOT NULL
编辑有完整的查询,阅读后的评论:]
SELECT COUNT(*), 'null_tally' AS narrative FROM us WHERE a IS NULL UNION SELECT COUNT(*), 'not_null_tally' AS narrative FROM us WHERE a IS NOT NULL;
这是一个适用于Oracle的快速和肮脏的版本:
select sum(case a when null then 1 else 0) "Null values", sum(case a when null then 0 else 1) "Non-null values" from us
正如我理解你的查询,你只需运行这个脚本,并获得总空,总NotNull行,
select count(*) - count(a) as 'Null', count(a) as 'Not Null' from us;
通常我使用这个技巧
select sum(case when a is null then 0 else 1 end) as count_notnull, sum(case when a is null then 1 else 0 end) as count_null from tab group by a
对于非空值
select count(a) from us
为空值
select count(*) from us minus select count(a) from us
于是
SELECT COUNT(A) NOT_NULLS FROM US UNION SELECT COUNT(*) - COUNT(A) NULLS FROM US
应该做这个工作
这有点棘手。 假设表格只有一列,那么Count(1)和Count(*)会给出不同的值。
set nocount on declare @table1 table (empid int) insert @table1 values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(NULL),(11),(12),(NULL),(13),(14); select * from @table1 select COUNT(1) as "COUNT(1)" from @table1 select COUNT(empid) "Count(empid)" from @table1
查询结果
正如您在图像中看到的那样,第一个结果显示该表格有16行。 其中两行是NULL。 所以当我们使用Count(*)查询引擎计算行数时,我们得到的计数结果为16.但是在Count(empid)的情况下,它计算了empid列中的非NULL值。 所以我们得到了14的结果。
所以无论何时我们使用COUNT(Column)确保我们照顾NULL值如下所示。
select COUNT(isnull(empid,1)) from @table1
将统计NULL和非NULL值。
注意 :即使表格由多个列组成,也是如此。 计数(1)将给出总行数,而不考虑NULL /非NULL值。 只有在使用Count(Column)计算列值时,我们需要注意NULL值。
我有一个类似的问题:计数所有不同的值,计数空值也为1。 在这种情况下,简单的计数不起作用,因为它不考虑空值。
这是一个适用于SQL的片段,不涉及新值的select。 基本上,一旦执行了distinct,还会使用row_number()函数在新列(n)中返回行号,然后对该列执行计数:
SELECT COUNT(n) FROM ( SELECT *, row_number() OVER (ORDER BY [MyColumn] ASC) n FROM ( SELECT DISTINCT [MyColumn] FROM [MyTable] ) items ) distinctItems
如果您使用MS Sql Server …
SELECT COUNT(0) AS 'Null_ColumnA_Records', ( SELECT COUNT(0) FROM your_table WHERE ColumnA IS NOT NULL ) AS 'NOT_Null_ColumnA_Records' FROM your_table WHERE ColumnA IS NULL;
我不build议你这样做,但是在这里你可以得到它(和结果一样)
使用ISNULLembedded式function。
这里有两个解决scheme:
Select count(columnname) as countofNotNulls, count(isnull(columnname,1))-count(columnname) AS Countofnulls from table name
要么
Select count(columnname) as countofNotNulls, count(*)-count(columnname) AS Countofnulls from table name
如果它的MySQL,你可以尝试这样的事情。
select (select count(*) from TABLENAME WHERE a = 'null') as total_null, (select count(*) from TABLENAME WHERE a != 'null') as total_not_null FROM TABLENAME
SELECT SUM(NULLs) AS 'NULLS', SUM(NOTNULLs) AS 'NOTNULLs' FROM (select count(*) AS 'NULLs', 0 as 'NOTNULLs' FROM us WHERE a is null UNION select 0 as 'NULLs', count(*) AS 'NOTNULLs' FROM us WHERE a is not null) AS x
这很好看,但它会返回一个logging,其中2列表示空值与非空值的计数。
以防万一你想在一个单一的logging:
select (select count(*) from tbl where colName is null) Nulls, (select count(*) from tbl where colName is not null) NonNulls
😉
select count(isnull(NullableColumn,-1))
尝试
SELECT SUM(ISNULL(a)) AS all_null, SUM(!ISNULL(a)) AS all_not_null FROM us;
简单!
这在T-SQL中起作用。 如果你只是数了一些东西,你想包括空值,使用COALESCE而不是大小写。
IF OBJECT_ID('tempdb..#us') IS NOT NULL DROP TABLE #us CREATE TABLE #us ( a INT NULL ); INSERT INTO #us VALUES (1),(2),(3),(4),(NULL),(NULL),(NULL),(8),(9) SELECT * FROM #us SELECT CASE WHEN a IS NULL THEN 'NULL' ELSE 'NON-NULL' END AS 'NULL?', COUNT(CASE WHEN a IS NULL THEN 'NULL' ELSE 'NON-NULL' END) AS 'Count' FROM #us GROUP BY CASE WHEN a IS NULL THEN 'NULL' ELSE 'NON-NULL' END SELECT COALESCE(CAST(a AS NVARCHAR),'NULL') AS a, COUNT(COALESCE(CAST(a AS NVARCHAR),'NULL')) AS 'Count' FROM #us GROUP BY COALESCE(CAST(a AS NVARCHAR),'NULL')
我build立了阿尔贝托,增加了汇总。
SELECT [Narrative] = CASE WHEN [Narrative] IS NULL THEN 'count_total' ELSE [Narrative] END ,[Count]=SUM([Count]) FROM (SELECT COUNT(*) [Count], 'count_nulls' AS [Narrative] FROM [CrmDW].[CRM].[User] WHERE [EmployeeID] IS NULL UNION SELECT COUNT(*), 'count_not_nulls ' AS narrative FROM [CrmDW].[CRM].[User] WHERE [EmployeeID] IS NOT NULL) S GROUP BY [Narrative] WITH CUBE;
a为空的元素数量:
select count(a) from us where a is null;
a不为null的元素数量:
select count(a) from us where a is not null;
用于计数非空值
select count(*) from us where a is not null;
用于计数空值
select count(*) from us where a is null;