我有 3 列,比如说A
、B
和C
。我需要计算NULL
每列中的值。
例如:
A | B | C
-------------
1 |NULL| 1
1 | 1 | NULL
NULL| 1 | 1
NULL|NULL| 1
应该输出:
A | B | C
---------------
2 | 2 | 1
我已经尝试过计数、求和、子查询,但还没有对我有用。任何输入将不胜感激!
我有 3 列,比如说A
、B
和C
。我需要计算NULL
每列中的值。
例如:
A | B | C
-------------
1 |NULL| 1
1 | 1 | NULL
NULL| 1 | 1
NULL|NULL| 1
应该输出:
A | B | C
---------------
2 | 2 | 1
我已经尝试过计数、求和、子查询,但还没有对我有用。任何输入将不胜感激!
SELECT COUNT(*)-COUNT(A) As A, COUNT(*)-COUNT(B) As B, COUNT(*)-COUNT(C) As C
FROM YourTable;
对于 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
您将收到一个结果集,其中包含表的每一列中 Null 值和非 Null 值的计数
您可以将聚合函数与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
看演示
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
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
我对此很晚了,但是如果您不想手动列出所有列名并且仍想获取表,则可以在 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