SQL计算表中所有列的不重复值并计算空值

时间:2019-05-24 13:17:44

标签: mysql sql multiple-columns isnull distinct-values

我有一个名为tbl_site的表,其中有50列。我想编写一些SQL代码,该代码将计算每列的不同值和空值的数量,而不必为每列运行一条语句。

我知道这可能包括对information_schema.columns运行嵌套查询,但是我不确定如何进一步构造查询。此外,如果可能,空值将包含“”和“”的值。

所需的输出如下:

Column      | Distinct | Null
site_id     | 100      | 0
sitearea_id | 12       | 0
site_area   | 54       | 5
etc....

3 个答案:

答案 0 :(得分:0)

尝试混合使用区分大小写和总和大小写:

SELECT Column, count(distinct Column) as 'Distinct'
,sum(case when Column is null then 1 else 0 end) as 'Null'
FROM  tbl_site
GROUP BY 1

答案 1 :(得分:0)

是的,我在为SQL Server编写脚本后注意到它是MySQL ...但是无论如何,这里是万一有人需要它的代码...或者如果您从中知道如何做

declare @position int = 1,
        @sql nvarchar(max),
        @columnCnt int,
        @currentColumn nvarchar(50),
        @TableName nvarchar(50) = 'YourTableName',
        @DBName nvarchar(50) = 'YourDbName';

if (OBJECT_ID('tempdb..#MyRowCount')) IS NOT NULL DROP TABLE #MyRowCount
CREATE TABLE #MyRowCount (ColumnName nvarchar(50), DistinctCount int, NullCount int)

set @columnCnt = (select MAX(ORDINAL_POSITION) from INFORMATION_SCHEMA.COLUMNS 
                  where TABLE_NAME = @TableName and TABLE_CATALOG = @DBName)

WHILE (@position <= @columnCnt)
BEGIN

    set @currentColumn = (select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS 
                          where TABLE_NAME = @TableName and 
                                TABLE_CATALOG = @DBName and 
                                ORDINAL_POSITION = @position)

    set @sql = 'INSERT INTO #MyRowCount (ColumnName, DistinctCount, NullCount)
                  SELECT ''' + @currentColumn + ''', 
                 (SELECT COUNT(DISTINCT [' + @currentColumn + ']) FROM ' + @TableName + ' where [' + @currentColumn + '] IS NOT NULL), 
                 (SELECT COUNT(*) FROM ' + @TableName + ' where [' + @currentColumn + '] IS NULL)';

    -- print @sql;

    execute (@sql);

    set @position = @position + 1;

END

SELECT * FROM #MyRowCount

答案 2 :(得分:0)

在MySQL中,您可以使用以下命令构造查询:

set @sql = '
select ''[column]'' as col, count(distinct "[column]"), sum("[column]" is null)
from [table] t
';

select group_concat(replace(replace(@sql, '[table]', table_name), '[column]', column_name) separator ' union all ')
from information_schema.columns
where table_name = ?;

此方法的警告是,您需要确保group_concat的最大长度值足够长(默认值1024不会使您走得太远)。

然后,您可以复制查询以使用prepare / execute来运行查询。