使用SQL计算多列中的NULL值

时间:2013-05-13 18:27:00

标签: sql

我有3列,比如ABC。我需要计算每列中的NULL值。

例如:

 A  | B  | C
-------------
1   |NULL| 1
1   | 1  | NULL
NULL| 1  | 1
NULL|NULL| 1

应输出:

 A  |  B  |  C
---------------
 2  |  2  |  1

我已经尝试过计算,求和,子查询但是没有任何东西对我有效。任何意见都将不胜感激!

6 个答案:

答案 0 :(得分:21)

SELECT COUNT(*)-COUNT(A) As A, COUNT(*)-COUNT(B) As B, COUNT(*)-COUNT(C) As C
FROM YourTable; 

答案 1 :(得分:6)

您可以使用带有CASE表达式的聚合函数:

select 
  sum(case when a is null then 1 else 0 end) A,
  sum(case when b is null then 1 else 0 end) B,
  sum(case when c is null then 1 else 0 end) C
from yt

请参阅Demo

答案 2 :(得分:1)

对于SQL SERVER,您可以使用以下内容:

SET NOCOUNT ON
DECLARE @Schema NVARCHAR(100) = '<Your Schema>'
DECLARE @Table NVARCHAR(100) = '<Your Table>'
DECLARE @sql NVARCHAR(MAX) =''
IF OBJECT_ID ('tempdb..#Nulls') IS NOT NULL DROP TABLE #Nulls

CREATE TABLE #Nulls (TableName sysname, ColumnName sysname , ColumnPosition int ,NullCount int , NonNullCount int)

 SELECT @sql += 'SELECT  '''+TABLE_NAME+''' AS TableName , '''+COLUMN_NAME+''' AS ColumnName,  '''+CONVERT(VARCHAR(5),ORDINAL_POSITION)+''' AS ColumnPosition, SUM(CASE WHEN '+COLUMN_NAME+' IS NULL THEN 1 ELSE 0 END) CountNulls , COUNT(' +COLUMN_NAME+') CountnonNulls FROM '+QUOTENAME(TABLE_SCHEMA)+'.'+QUOTENAME(TABLE_NAME)+';'+ CHAR(10)
 FROM INFORMATION_SCHEMA.COLUMNS
 WHERE TABLE_SCHEMA = @Schema
 AND TABLE_NAME = @Table

 INSERT INTO #Nulls 
 EXEC sp_executesql @sql

 SELECT * 
 FROM #Nulls

 DROP TABLE #Nulls

您将收到一个结果集,其中包含您表中每列的空值和非空值

答案 3 :(得分:1)

SELECT
(SELECT count(*) FROM my_table WHERE A is NULL) as A,
(SELECT count(*) FROM my_table WHERE B is NULL) as B,
(SELECT count(*) FROM my_table WHERE C is NULL) as C

答案 4 :(得分:0)

select sum(case when a is null then 1 else 0 end) as a_null_count
    , sum(case when b is null then 1 else 0 end) as b_null_count
    , sum(case when c is null then 1 else 0 end) as c_null_count
from table

答案 5 :(得分:0)

我已经很晚了,但是如果您不想手动列出所有列名但仍想获取表,您可以在 SQL Server 中执行此操作(只需将 testTable 替换为您的实际表):

--Creates the table the poster wanted

CREATE TABLE testTable (A int, B int, C int)
INSERT INTO testTable (A, C) VALUES (1,1)
INSERT INTO testTable (A, B) VALUES (1,1)
INSERT INTO testTable (B, C) VALUES (1,1)
INSERT INTO testTable (C) VALUES (1)

--Creates the output table which will consist of each column name and the amount of nulls

CREATE TABLE ColumnNames (
    ID int IDENTITY(1,1) PRIMARY KEY,
    [name] varchar(max),
    nullAmount int
)

INSERT INTO ColumnNames ([name]) 
SELECT [name] FROM sys.columns WHERE object_id = OBJECT_ID('dbo.testTable')

DECLARE @columnIndex INT = 1

WHILE @columnIndex <= ( SELECT COUNT(*) FROM dbo.ColumnNames )
BEGIN
    DECLARE @colName nvarchar(max) = (SELECT [name] FROM ColumnNames WHERE ID = @columnIndex)
    EXEC('SELECT ' + @colName + ' INTO colTable FROM testTable')

    DECLARE @SQL nvarchar(max) = N'UPDATE ColumnNames SET nullAmount = (SELECT COUNT(1) - COUNT(' + quotename(@colName) + ') FROM colTable) WHERE ID = @columnIndex'
    EXEC SP_EXECUTESQL @SQL, N'@columnIndex int', @columnIndex 

    DROP TABLE colTable
    SET @columnIndex = @columnIndex + 1
END

--Select the output table and get null info
SELECT * FROM ColumnNames