0

我有 15 个表,它们都有列 CreationDate 和 LastModifiedDate 有没有办法查询所有表并在表中查找空值。

这是我认为我必须做的,但想知道是否有更有效或更简单的方法来执行以下操作

Select COUNT(*) FROM Table1, Table2, Table3
WHERE table1.CreationDate IS NULL
      OR table1.LastModifiedDate IS NULL
      OR table2.CreationDate IS NULL
      OR table2.LastModifiedDate IS NULL
      OR table3.CreationDate IS NULL
      OR table3.LastModifiedDate IS NULL
4

2 回答 2

3
select count(*)
from (
    select CreationDate, LastModifiedDate from Table1
    union all
    select CreationDate, LastModifiedDate from Table2
    union all
    select CreationDate, LastModifiedDate from Table3
) a 
where CreationDate is null or LastModifiedDate is null
于 2012-08-09T15:45:09.487 回答
0

您的原始查询不符合您的预期。它在所有具有缺失值的行之间进行交叉连接。这可能会导致大量数据(如果所有表都有多行)或根本没有数据(如果一个没有丢失的行)。

我假设您想知道缺少东西的表名:

select tablename, count(*),
       sum(case when CreationDate is null then 1 else 0 end) as MissingCreationDate,
       sum(case when LastModifiedDate is null then 1 else 0 end) as MissingLastModifiedDate
from ((select 'table1' as tablename,  CreationDate, LastModifiedDate
       from Table1 
      ) union all
      (select 'table2' as tablename, CreationDate, LastModifiedDate
       from Table2
      ) union all
      . . .
      (select 'table15', CreationDate, LastModifiedDate
       from Table3
      ) 
     ) t
where CreationDate is null or LastModifiedDate is null
group by tablename
于 2012-08-09T15:58:48.907 回答