1

I need to be able to count all the NULLS in all columns of an Access 2010 table. what I mean by that specifically, is that of the 30 columns (fields) there are loads of records with partial data.

I want to count in the entire table how many are empty.

I read the article on this site titled, How to count all NULL values in table, but that referred to SQL and also, was, I am sorry to admit too complex for me.

Anyone got any futher clues?

:)

4

3 回答 3

4

对于单个字段,您可以使用简单查询。

SELECT Count(*) AS CountOfNulls
FROM MyTable
WHERE some_field Is Null;

如果要在单个查询中分别计算多个字段的 Null,可以执行以下操作:

SELECT
    Sum(IIf(some_field Is Null, 1, 0)) AS NullsIn_some_field,
    Sum(IIf(another_field Is Null, 1, 0)) AS NullsIn_another_field
FROM MyTable;

如果您想要所有 Null 的总计,而不是每列的计数,您可以将前一个查询用作子查询并将各个列计数相加。

SELECT base.NullsIn_some_field + base.NullsIn_another_field AS total_nulls
FROM
    (
        SELECT
            Sum(IIf(some_field Is Null, 1, 0)) AS NullsIn_some_field,
            Sum(IIf(another_field Is Null, 1, 0)) AS NullsIn_another_field
        FROM MyTable
    ) AS base;

OTOH,如果您希望完全避免使用 SQL,或者只是觉得这些语句太复杂,则不需要使用 SQL。您可以DCount()在 VBA 过程中使用该函数。

使用数据库中表的名称在“立即”窗口中运行以下过程:

HowManyNulls "YourTable"

CTRL您可以使用+g键盘快捷键转到“立即”窗口。

Public Sub HowManyNulls(ByVal pTable As String)
    Dim db As DAO.Database
    Dim fld As DAO.Field
    Dim tdf As DAO.TableDef
    Dim lngNulls As Long
    Dim lngTotal As Long

    Set db = CurrentDb
    Set tdf = db.TableDefs(pTable)
    For Each fld In tdf.Fields
        'lngNulls = DCount("*", pTable, fld.Name & " Is Null")
        ' accommodate field names which need bracketing ...
        lngNulls = DCount("*", pTable, "[" & fld.Name & "] Is Null")
        lngTotal = lngTotal + lngNulls
        Debug.Print fld.Name, lngNulls
    Next fld
    Debug.Print "Grand total", lngTotal
    Set fld = Nothing
    Set tdf = Nothing
    Set db = Nothing
End Sub

如果这些建议都不令人满意,请修改您的问题以帮助我们更好地了解您的需求。

于 2012-05-11T17:42:30.160 回答
1

当您事先不知道列名时,没有简单的方法可以跨表的“所有列”执行这种计数。

如果你想这样做,你必须编写一个程序来读取数据库的“元数据”,从该数据中提取列和表的列表,为每一列构建一个单独的 SQL 语句,然后执行该语句并添加您可以返回找到的 NULL 值的运行总数。

这听起来像是您可能考虑追求的东西吗?如果是这样,您应该搜索有关获取 MS Access 数据库中的表列表和获取 Access 数据库中表的列列表的解决方案,然后将该信息与您已经从有关计算 NULL 的问题中获得的信息相结合在一列中。

于 2012-05-11T18:31:35.500 回答
0

要计算所有列的表中的空值:

select count(*) from(select a from tt where a is  null
              union all
            select b from tt   where b is  null
              union all
            select c from tt where c is  null)
于 2015-12-30T18:38:25.680 回答