[英]Number of Null and non-null values for each column in SQL for a given table
I am trying to find the number of null values, non-null values, total rows for each column in a table named StudentScore
.我正在尝试查找名为
StudentScore
的表中每列的 null 值、非空值、总行数。
Code to create table:创建表的代码:
CREATE TABLE StudentScore
( Student_ID INT PRIMARY KEY,
Student_Name NVARCHAR (50),
Student_Score INT )
GO INSERT INTO StudentScore VALUES (1,'Ali', NULL)
INSERT INTO StudentScore VALUES (2,'Zaid', 770)
INSERT INTO StudentScore VALUES (3,'Mohd', 1140)
INSERT INTO StudentScore VALUES (4,NULL, 770)
INSERT INTO StudentScore VALUES (5,'John', 1240)
INSERT INTO StudentScore VALUES (6,'Mike', 1140)
INSERT INTO StudentScore VALUES (7,'Goerge', NULL)
Code to find number of null, non null, total rows for each column in the table:查找 null、非 null、表中每列的总行数的代码:
DECLARE @sql NVARCHAR(MAX);
SELECT @sql = STRING_AGG(
FORMATMESSAGE('SELECT table_schema = ''%s''
,table_name = ''%s''
,table_col_name = ''%s''
,row_num = COUNT(*)
,row_num_non_nulls = COUNT(%s)
,row_num_nulls = COUNT(*) - COUNT(%s)
FROM %s.%s',
QUOTENAME(dbo),
QUOTENAME(StudentScore),
QUOTENAME(COLUMN_NAME),
QUOTENAME(COLUMN_NAME),
QUOTENAME(COLUMN_NAME),
QUOTENAME(dbo),
QUOTENAME(StudentScore),
QUOTENAME(COLUMN_NAME)), ' UNION ALL' + CHAR(13)
) WITHIN GROUP(ORDER BY dbo, StudentScore)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE IS_NULLABLE = 'YES';
SELECT @sql;
EXEC(@sql);
I am getting below errors:我收到以下错误:
Msg 207, Level 16, State 1, Line 11
Invalid column name 'dbo'.
Msg 207, Level 16, State 1, Line 12
Invalid column name 'StudentScore'.
Msg 207, Level 16, State 1, Line 16
Invalid column name 'dbo'.
Msg 207, Level 16, State 1, Line 17
Invalid column name 'StudentScore'.
Msg 207, Level 16, State 1, Line 19
Invalid column name 'dbo'.
Msg 207, Level 16, State 1, Line 19
Invalid column name 'StudentScore'.
Result I need:结果我需要:
| table_schema | table_name | table_col_name | row_num | row_num_non_nulls | row_num_nulls |
+---------------+-----------------+------------------+----------+--------------------+---------------+
| [dbo] | [StudentScore] | [Student_Name] | 7 | 6 | 1 |
| [dbo] | [StudentScore] | [Student_Score] | 7 | 5 | 2 |
The reason for the error is that INFORMATION_SCHEMA.COLUMNS
does not contain columns dbo
and StudentTable
, so using QUOTENAME(dbo)
, QUOTENAME(StudentScore)
and WITHIN GROUP(ORDER BY dbo, StudentScore)
is an eror.错误的原因是
INFORMATION_SCHEMA.COLUMNS
不包含列dbo
和StudentTable
,因此使用QUOTENAME(dbo)
、 QUOTENAME(StudentScore)
和WITHIN GROUP(ORDER BY dbo, StudentScore)
是一个错误。
If you want to count the NULL
and NOT NULL
values for NULLABLE
columns for one specific table, the statement should be:如果要计算一个特定表的
NULLABLE
列的NULL
和NOT NULL
值,则语句应为:
SELECT @sql = STRING_AGG(
FORMATMESSAGE('
SELECT table_schema = ''%s''
,table_name = ''%s''
,table_col_name = ''%s''
,row_num = COUNT(*)
,row_num_non_nulls = COUNT(%s)
,row_num_nulls = COUNT(*) - COUNT(%s)
FROM %s.%s
',
QUOTENAME(c.TABLE_SCHEMA),
QUOTENAME(c.TABLE_NAME),
QUOTENAME(c.COLUMN_NAME),
QUOTENAME(c.COLUMN_NAME),
QUOTENAME(c.COLUMN_NAME),
QUOTENAME(c.TABLE_SCHEMA),
QUOTENAME(c.TABLE_NAME)
),
' UNION ALL '
) WITHIN GROUP(ORDER BY c.TABLE_SCHEMA, c.TABLE_NAME)
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE
(c.TABLE_SCHEMA = 'dbo') AND
(c.TABLE_NAME = 'StudentScore') AND
(c.IS_NULLABLE = 'YES');
SELECT @sql;
EXEC (@sql);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.