需要为每条记录查找具有空值的所有列 - 访问 SQL 或 vba
Posted
技术标签:
【中文标题】需要为每条记录查找具有空值的所有列 - 访问 SQL 或 vba【英文标题】:Need to find all columns with null values for every record - Access SQL or vba 【发布时间】:2017-03-22 15:30:18 【问题描述】:如果这个问题已经存在,我深表歉意 - 我已尝试寻找解决方案,但尚未找到解决方案。
我有一个数据库模板,用于设置单独的数据库来处理不总是采用相同格式或有时具有附加字段的客户端数据。我正在尝试构建一个查询来测试累积表的结果,并确保设置新数据库的分析师没有遗漏任何内容。最终我只需要一种方法来显示累积表中是否有任何字段对于每条记录都是 NULL 并告诉我字段名称。
我尝试使用 SWITCH 和 IIF 设置查询,但涉及的字段太多,所以我总是收到 Expression Too Complex 错误。
这是一个查询示例,尽管我的数据库最终将有 70 个字段需要在设置后测试空值。
SELECT SWITCH(
TBL_Cumulative.[ScreeningDate] IS NULL, "ScreeningDate Missing",
TBL_Cumulative.[ScreeningLocation] IS NULL, "ScreeningLocation Missing",
TBL_Cumulative.[Source] IS NULL, "Source Missing",
TBL_Cumulative.[FirstName] IS NULL, "FirstName Missing",
TBL_Cumulative.[LastName] IS NULL, "LastName Missing",
TBL_Cumulative.[DOB] IS NULL, "DOB Missing",
TBL_Cumulative.[Female] IS NULL, "Female Missing",
TBL_Cumulative.[Male] IS NULL, "Male Missing",
TBL_Cumulative.[Height_FT] IS NULL, "Height_FT Missing",
TBL_Cumulative.[Height_IN] IS NULL, "Height_IN Missing",
TBL_Cumulative.[Weight] IS NULL, "Weight Missing",
TBL_Cumulative.[Fasting_Y] IS NULL, "Fasting_Y Missing",
TBL_Cumulative.[Fasting_N] IS NULL, "Fasting_N Missing",
TBL_Cumulative.[Pregnant_Y] IS NULL, "Pregnant_Y Missing",
TBL_Cumulative.[Pregnant_N] IS NULL, "Pregnant_N Missing",
TBL_Cumulative.[Tobacco_Y] IS NULL, "Tobacco_Y Missing",
TBL_Cumulative.[Tobacco_N] IS NULL, "Tobacco_N Missing",
TBL_Cumulative.[hbA1c] IS NULL, "hbA1c Missing",
TBL_Cumulative.[Cotinine] IS NULL, "Cotinine Missing",
TBL_Cumulative.[TSH] IS NULL, "TSH Missing",
TBL_Cumulative.[PSA] IS NULL, "PSA Missing") AS Error FROM TBL_Cumulative
【问题讨论】:
【参考方案1】:我认为您正在寻找 CASE 声明。
SELECT CASE WHEN TBL_Cumulative.[ScreeningDate] IS NULL THEN 'ScreeningDate Missing'
WHEN TBL_Cumulative.[ScreeningLocation] IS NULL THEN 'ScreeningLocation Missing'
WHEN TBL_Cumulative.[Source] IS NULL THEN 'Source Missing',
WHEN TBL_Cumulative.[FirstName] IS NULL THEN 'FirstName Missing',
WHEN TBL_Cumulative.[LastName] IS NULL THEN 'LastName Missing',
WHEN TBL_Cumulative.[DOB] IS NULL THEN 'DOB Missin',
WHEN TBL_Cumulative.[Female] IS NULL THEN 'Female Missing',
WHEN TBL_Cumulative.[Male] IS NULL THEN 'Male Missing',
WHEN TBL_Cumulative.[Height_FT] IS NULL THEN 'Height_FT Missing',
WHEN TBL_Cumulative.[Height_IN] IS NULL THEN 'Height_IN Missing',
WHEN TBL_Cumulative.[Weight] IS NULL THEN 'Weight Missing',
WHEN TBL_Cumulative.[Fasting_Y] IS NULL THEN 'Fasting_Y Missing',
WHEN TBL_Cumulative.[Fasting_N] IS NULL THEN 'Fasting_N Missing',
WHEN TBL_Cumulative.[Pregnant_Y] IS NULL THEN 'Pregnant_Y Missing',
WHEN TBL_Cumulative.[Pregnant_N] IS NULL THEN 'Pregnant_N Missing',
WHEN TBL_Cumulative.[Tobacco_Y] IS NULL THEN 'Tobacco_Y Missing',
WHEN TBL_Cumulative.[Tobacco_N] IS NULL THEN 'Tobacco_N Missing',
WHEN TBL_Cumulative.[hbA1c] IS NULL THEN 'hbA1c Missing',
WHEN TBL_Cumulative.[Cotinine] IS NULL THEN 'Cotinine Missing',
WHEN TBL_Cumulative.[TSH] IS NULL THEN 'TSH Missing'
END AS ERROR
FROM TBL_Cumulative
【讨论】:
很确定 CASE 不是 Access SQL 的一部分。【参考方案2】:这应该会为您创建查询。如果查询已经存在,则会抛出错误。
Public Sub Test()
CreateQuery "TBL_Cumulative"
End Sub
Public Sub CreateQuery(TableName As String)
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Dim SQL_Select As String
Dim SQL_Where As String
Dim SQL As String
Set db = CurrentDb
Set rst = db.OpenRecordset(TableName)
SQL_Select = "SELECT "
SQL_Where = "WHERE "
For Each fld In rst.Fields
SQL_Select = SQL_Select & "[" & fld.Name & "], "
SQL_Where = SQL_Where & "[" & fld.Name & "] Is Null OR "
Next fld
SQL_Select = Left(SQL_Select, Len(SQL_Select) - 2) & " FROM [" & TableName & "]"
SQL_Where = Left(SQL_Where, Len(SQL_Where) - 4)
SQL = SQL_Select & SQL_Where
Set qdf = db.CreateQueryDef("SQL_" & TableName, SQL)
rst.Close
Set rst = Nothing
Set qdf = Nothing
End Sub
【讨论】:
【参考方案3】:所以我所做的就是使用一点 excel 魔法。
-
从表 TBL_Cumulative 中选择一行。
将该行(包括标题)复制到 Excel 中。
在 Excel 中再次复制该行,使用转置粘贴到新工作表中。
现在您有了 A 列中所有字段的列表。
在 B 列中,创建一个公式:
=CONCAT("SELECT '";$A1;"', COUNT(*) FROM TBL_Cumulative WHERE ";$A1;" IS NOT NULL UNION ALL")
(从单元格的右下角)向下拖动到所有行(带有列名)以复制公式。
复制生成的文本。
粘贴到 Access 的查询窗口中。
从最后一行/文本行中删除最后一个“UNION ALL”。
运行查询。
结果将是一个列表,其中包含所有列的名称,以及它们有多少次非空值。所以你可以只挑出计数为 0(零)的那些。
【讨论】:
以上是关于需要为每条记录查找具有空值的所有列 - 访问 SQL 或 vba的主要内容,如果未能解决你的问题,请参考以下文章