3

我有一张有 40 列的表格。

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

例子,

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

我想要的输出是:

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

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

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

4

2 回答 2

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

教授

于 2013-04-23T09:24:47.040 回答
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)
于 2013-04-23T10:11:53.060 回答