查找所有列的空/空记录数

时间:2013-04-23 09:17:45

标签: sql sql-server

我有一张包含40列的表格。

我希望编写一个查询,它将返回每列中空值/空值的数量。

实施例,

如果有A,B,C和D列。

我想要的输出是:

 A|B|C|D
 3|5|4|9

其中3是记录数:     其中A为null或A ='' 等等...

任何帮助将不胜感激。 我正在使用MS SQL 2008 R2

2 个答案:

答案 0 :(得分:6)

试试这个 -

DECLARE @temp TABLE
(
      A VARCHAR(50)
    , B VARCHAR(50)
    , C VARCHAR(50)
    , D VARCHAR(50)
)

INSERT INTO @temp (A, B, C, D)
VALUES 
    ('', 'dr', '1', NULL),
    ('d', NULL, '45', 'h')

SELECT 
    A = COUNT(CASE WHEN ISNULL(A, '') = '' THEN 1 END)
  , B = COUNT(CASE WHEN ISNULL(B, '') = '' THEN 1 END)
  , C = COUNT(CASE WHEN ISNULL(C, '') = '' THEN 1 END)
  , D = COUNT(CASE WHEN ISNULL(D, '') = '' THEN 1 END)
FROM @temp

<强>更新

在这种情况下,请尝试动态sql -

DECLARE @TableName SYSNAME
SELECT @TableName = 'dbo.test1'

DECLARE @SQL NVARCHAR(MAX)

SELECT @SQL = 'SELECT' + CHAR(13) + STUFF((
    SELECT CHAR(13) + ', ' + c.name + ' = COUNT(CASE WHEN ISNULL(CAST(' + c.name + ' AS NVARCHAR(MAX)), '''') = '''' THEN 1 END)'
    FROM (
        SELECT o.[object_id]
        FROM sys.objects o
        JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
        WHERE o.[type] = 'U'
            AND s.name + '.' + o.name = @TableName
    ) o
    JOIN sys.columns c ON o.[object_id] = c.[object_id]
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, ' ') + CHAR(13) + 'FROM ' + @TableName

PRINT @SQL

EXEC sys.sp_executesql @SQL

在输出中你可以得到类似的东西:

SELECT
  WorkOutID = COUNT(CASE WHEN ISNULL(CAST(WorkOutID AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, TimeSheetDate = COUNT(CASE WHEN ISNULL(CAST(TimeSheetDate AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, DateOut = COUNT(CASE WHEN ISNULL(CAST(DateOut AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, EmployeeID = COUNT(CASE WHEN ISNULL(CAST(EmployeeID AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, IsMainWorkPlace = COUNT(CASE WHEN ISNULL(CAST(IsMainWorkPlace AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, DepartmentUID = COUNT(CASE WHEN ISNULL(CAST(DepartmentUID AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, WorkPlaceUID = COUNT(CASE WHEN ISNULL(CAST(WorkPlaceUID AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, WorkShiftCD = COUNT(CASE WHEN ISNULL(CAST(WorkShiftCD AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, TeamUID = COUNT(CASE WHEN ISNULL(CAST(TeamUID AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, WorkHours = COUNT(CASE WHEN ISNULL(CAST(WorkHours AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, AbsenceCode = COUNT(CASE WHEN ISNULL(CAST(AbsenceCode AS NVARCHAR(MAX)), '') = '' THEN 1 END)
, PaymentType = COUNT(CASE WHEN ISNULL(CAST(PaymentType AS NVARCHAR(MAX)), '') = '' THEN 1 END)
FROM dbo.test1

proff

答案 1 :(得分:1)

试试这个

DECLARE @cols1 NVARCHAR(MAX);
DECLARE @sql NVARCHAR(MAX);

SELECT @cols1 = STUFF((
    SELECT ', COUNT(CASE WHEN ISNULL(CONVERT(NVARCHAR(MAX), [' + t1.NAME + ']), '''') = '''' THEN 1 END) AS ' + t1.name
    FROM sys.columns AS t1
    WHERE t1.object_id = OBJECT_ID('myTable')
    --ORDER BY ', COUNT([' + t1.name + ']) AS ' + t1.name
    FOR XML PATH('')
), 1, 2, '');

SET @sql = '
SELECT ' + @cols1 + '
FROM myTable
'

EXEC(@sql)