我有以下代码来从C#中的SQL表中获取所有列:
public List<string> GetColumns(string tableName)
{
var columns = new List<string>();
using (var conn = new SqlConnection(_sqlServerConnectionString.SqlServerConnectionString))
{
conn.Open();
var selectQuery = $"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{tableName}'";
using (var cmd = new SqlCommand(selectQuery, conn))
{
using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
columns.Add(reader["COLUMN_NAME"].ToString());
}
}
conn.Close();
}
return columns;
}
在C#中,如何从这些列中找出NULL、NOT NULL和TOTAL值的数目?
例如:
COLUMN 1, COLUMN 2
X, A
Y, NULLL
Y, NULL
Z, B
NULL, C
NULL, NULL
所以对于COLUMN1
TOTAL # of values: 6
NULL: 2
NOT NULL: 4
所以对于COLUMN2
TOTAL # of values: 6
NULL: 3
NOT NULL: 3
所有列的合计应相同。
NULL
值被排除在除count()
之外的所有聚合函数之外,因此。要确定列中空/非空值的数量,
select sum( case when t.col_1 is null then 1 else 0 end ) as null_count,
sum( case when t.col_1 is null then 0 else 1 end ) as not_null_count,
count(*) as total
from someTable t