I have a SQL database that I am querying as part of a project - I only have read access to it.
There is a column called ResultStatus
- possible values are "Passed" and "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. 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
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:
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. 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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.