[英]How do you count misspelled fields using a SQL query?
I have a SQL database that I am querying as part of a project - I only have read access to it. 我有一个要查询的SQL数据库,它是项目的一部分-我对此只有读取权限。
There is a column called ResultStatus
- possible values are "Passed" and "Failed". 有一个名为
ResultStatus
的列-可能的值为“ Passed”和“ Failed”。 However, there were some typos by the original data inputter so some of them say "Fialed" as well. 但是,原始数据输入者有一些错别字,因此其中一些人也说“已完成”。 I want to count the number of "Failed" entries, but I want to include the "Fialed" ones as well.
我想计算“失败”条目的数量,但是我也想包括“已完成”条目。
SELECT
ResultStatus, Count(*)
FROM
[DB_018].[dbo].[ProjectData]
GROUP BY ResultStatus
is obviously grouping "Fialed" in a different category. 显然将“ Fialed”分组在另一个类别中。 I want it to be counted along with "Failed".
我希望将其与“失败”一起计算在内。
You can correct the spelling yourself 您可以自己更正拼写
SELECT Case When ResultStatus = 'Fialed' then 'Failed' Else ResultStatus End AS ResultStatus, Count(*)
FROM [DB_018].[dbo].[ProjectData]
GROUP BY Case When ResultStatus = 'Fialed' then 'Failed' Else ResultStatus End
What this is doing is replacing the incorrect spelling with the correct one while you group the data. 这是在对数据进行分组时用正确的拼写替换错误的拼写。
Note that this is possible, and possibly cleaner, to do using a CTE 请注意,使用CTE可以这样做,而且可能更清洁
with CleanedResults as (
select
case
when ResultStatus = 'Fialed' then 'Failed'
when ResultStatus = 'Pased' then 'Passed'
else ResultStatus
end as ResultStatus
from [DB_018].[dbo].[ProjectData]
) select
ResultStatus
, count(*) as NumResults
from CleanedResults
group by ResultStatus
I'd use: 我会用:
SELECT
case when left(ResultStatus,1) = 'P' then 'Pass'
when left(ResultStatus,1) = 'a' then 'audit'
else 'fail' end as result, Count(*)
FROM
ProjectData
GROUP BY left(ResultStatus,1)
as COUNT will not really count NULL values, then you can use CASE statement and just writes as below: 因为COUNT不会真正计算NULL值,所以您可以使用CASE语句,只需编写如下:
SELECT COUNT(CASE WHEN ResultStatus = 'Fialed' THEN 1
END) as MissSpelledFailed,
COUNT(CASE WHEN ResultStatus = 'Pased' THEN 1
END) as MisSpelledPassed,
COUNT(CASE WHEN ResultStatus = 'Failed' THEN 1
END) as CorrectSpelledFailed,
COUNT(CASE WHEN ResultStatus = 'Passed' THEN 1
END) as CorrectSpelledPassed,
FROM [DB_018].[dbo].[ProjectData]
You need to get a distinct list of ResultStatus and add them all to the case statement below. 您需要获取ResultStatus的不同列表,并将它们全部添加到下面的case语句中。 I prefer this method to Raj's as you don't need to use a CTE (not available in all version of SQL Server) or adjusting the group by.
我不需要Raj的方法,因为您不需要使用CTE(并非在所有版本的SQL Server中都可用)或调整分组依据。
SELECT
ResultStatus,count(*) [Count]
FROM(
SELECT
CASE
WHEN ResultStatus = 'FIAL' THEN 'FAIL'
WHEN ResultStatus = 'FAIL' THEN 'FAIL'
WHEN ResultStatus = 'Passed' THEN 'Passed'
END [ResultStatus]
FROM [DB_018].[dbo].[ProjectData]
)a
GROUP BY ResultStatus
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.