由于某些奇怪的情况,我的Microsoft Access数据库中的随机长文本字段似乎正在损坏并被替换为" ################" 。我希望能够使用SQL查询计算损坏字段的数量,以便我可以快速检查数字是否已更改。
我编写了一个查询,可以计算值已损坏的记录数,但不计算总字段数(例如,如果5条记录有13个已损坏的值,我可以得到数字5,但我希望总数为13)。如何调整查询?
SELECT Count(*) AS [Number of Errors]
FROM GPInformation
WHERE Profile="################"
OR Notes="################"
OR CriminalConvictionsNotes="################"
OR HealthIssueNotes="################"
OR NextOfKinAddress="################"
输出:
答案 0 :(得分:2)
以下是unpivoting
数据
SELECT Count(error_data) AS [Number of Errors]
FROM (SELECT 1 AS error_Data
FROM gpinformation
WHERE profile = "################"
UNION ALL
SELECT 1
FROM gpinformation
UNION ALL
SELECT 1
FROM gpinformation
WHERE notes = "################"
UNION ALL
SELECT 1
FROM gpinformation
WHERE criminalconvictionsnotes = "################"
UNION ALL
SELECT 1
FROM gpinformation
WHERE healthissuenotes = "################"
UNION ALL
SELECT 1
FROM gpinformation
WHERE nextofkinaddress = "################") A
答案 1 :(得分:2)
您可以按如下方式重写查询:
SELECT SUM(
IIf(Profile="################", 1, 0) +
IIf(Notes="################", 1, 0) +
IIf(CriminalConvictionsNotes="################", 1, 0) +
IIf(HealthIssueNotes="################", 1, 0) +
IIf(NextOfKinAddress="################", 1, 0))
AS [Number of Errors]
FROM GPInformation
WHERE Profile="################"
OR Notes="################"
OR CriminalConvictionsNotes="################"
OR HealthIssueNotes="################"
OR NextOfKinAddress="################"
我没有测试过这段代码(缺乏立即设施),但它应该起作用或至少非常接近正确。
答案 2 :(得分:0)
您可以向select
添加条件逻辑:
SELECT (sum(iif(Profile = "################", 1, 0)) +
sum(iif(Notes = "################", 1, 0)) +
sum(iif(CriminalConvictionsNotes = "################", 1, 0)) +
sum(iif(HealthIssueNotes = "################", 1, 0)) +
sum(iif(NextOfKinAddress = "################", 1, 0))
) AS NumBadValues
FROM GPInformation
WHERE Profile = "################" OR
Notes = "################" OR
CriminalConvictionsNotes = "################" OR
HealthIssueNotes = "################" OR
NextOfKinAddress = "################";