How do you count misspelled fields using a SQL query? - sql

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

Related

count case with multiple conditions in same row

I need to export data of status from s.status column like
select s.name,
count(CASE WHEN s.STATUS = 'Active' THEN 1 END) AS Active_count,
count(CASE WHEN s.STATUS = 'Expired' THEN 1 END) AS Expired_count,
count(CASE WHEN s.STATUS = 'In Progress' THEN 1 END) AS InProgress_count
from my.table s
group by s.name,s.status
I expect it to be counted in one row but instead I am getting smthing like this
https://i.stack.imgur.com/K4wyc.png
Can anyone help me write it so I can get the data in one row ?
Thank you
Remove s.status from your group by

SQL Lag and LEAD query

I need data which is in Output column. When 1st column status is P then we need value from Filled date. But once status is anything from P then we need date from last P status. Pls. let me know if i am not able to explain. Thanks in advance.
In standard SQL, you can use:
select (case when status = 'P'
then filled_dt
else lag(case when status = 'P' then filled_dt end) over (partition by mbr_id order by filled_dt ignore nulls)
end) as imputed_filled_dt
This is standard SQL; however, not all databases support ignore nulls. This probably does what you want:
select (case when status = 'P'
then filled_dt
else max(case when status = 'P' then filled_dt end) over (partition by mbr_id order by filled_dt)
end) as imputed_filled_dt

Case Statement having no effect on output

I am trying to get sums of donations based on the bank approval status and grouped by gift kind. However, it the script outputs RR and NR donations on separate lines (see below the script). It seems like the case statements aren't working at all.
select gift_kind,
case
when c.bank_approval_status = 'AP' then
sum(c.charge_amount) end approved,
case
when c.bank_approval_status in ('RR','NR') then
sum(c.charge_amount) end rejected,
case
when c.bank_approval_status = 'AR' then
sum(c.charge_amount)*-1 end refunded,
case
when c.bank_approval_status not in ('AR','AP','RR','NR') then
sum(c.charge_amount) end other_status
from charge_log c, transactions t
where c.account_id=t.account_id
and c.process_id= 'CHG - 02532'
and c.gift_date=t.gift_date
and c.gift_seq=t.gift_seq
and C.PLEDGE_NUMBER=t.pledge_number
and t.sts='A'
group by t.fund_type, t.gift_kind, c.bank_approval_status
order by gift_kind asc
I believe you are looking for this logic:
select gift_kind,
sum(case when c.bank_approval_status = 'AP' then c.charge_amount
end) as approved,
sum(case when c.bank_approval_status in ('RR', 'NR') then c.charge_amount
end) as rejected,
sum(case when c.bank_approval_status in ('AR') then c.charge_amount*-1
end) as refunded,
sum(case when c.bank_approval_status not in ('AR','AP','RR','NR') then c.charge_amount
end) other_status
from charge_log c join
transactions t
on c.account_id = t.account_id and
c.gift_date = t.gift_date and
c.gift_seq = t.gift_seq and
C.PLEDGE_NUMBER = t.pledge_number
where c.process_id = 'CHG - 02532' and t.sts = 'A'
group by t.fund_type, t.gift_kind
order by gift_kind asc
Notes:
Learn to use proper JOIN syntax. Never use commas in the FROM clause.
The JOIN conditions should all be in the ON clause, not the WHERE clause.
The CASE is an argument to the SUM().
Remove the bank_approval_status from the GROUP BY.
I don't know why fund_type is in the GROUP BY. You may have a reason for that so I left it.
You need to put the case clauses inside the sums, and add else 0 to make sure you don't get null as a result:
sum(case
when c.bank_approval_status = 'AP' then
c.charge_amount else 0 end) approved,
sum(case
when c.bank_approval_status in ('RR','NR') then
c.charge_amount else 0 end) rejected,
sum(case
when c.bank_approval_status = 'AR' then
-c.charge_amount else 0 end) refunded,
sum(case
when c.bank_approval_status not in ('AR','AP','RR','NR') then
c.charge_amount else 0 end) other_status
And your group by should rarely be on columns you use in aggregations (here bank_approval_status in the sum). Change to:
group by gift_kind
Depending on your scheme, other fields might need to be added to the group by clause but then it would make sense to also put them in the select and order by clauses.

How can I make two column from same table by two query

I've two query from same table but by two condition but how can I make two column for this two conditional count.
SELECT Count(*) FROM TBL_FT WHERE STATUS = 'X';
SELECT Count(*) FROM TBL_FT WHERE STATUS = 'Y' and
LOGDATE>trunc(sysdate);
You can use conditional aggregation:
SELECT
COUNT(CASE WHEN STATUS = 'X' THEN 1 END),
COUNT(CASE WHEN STATUS = 'Y' AND LOGDATE > trunc(sysdate) THEN 1 END)
FROM TBL_FT
You can also add a WHERE clause:
WHERE STATUS IN ('X', 'Y');
you can use something like this -
SELECT SUM(CASE
WHEN STATUS = 'X' THEN
1
ELSE
0
END) FIRST_VAL,
SUM(CASE
WHEN STATUS = 'Y'
AND LOGDATE > TRUNC(SYSDATE) THEN
1
ELSE
0
END) second_val
FROM TBL_FT;

Merging data SQL Query

I have a query request where I have to show one customer activity for each web-site but it has to be only one row each, instead of one customer showing multiple times for each activity.
Following is the query I tried but brings lot more rows. please help me as how I can avoid duplicates and show only one customer by each row for each activity.
SELECT i.customer_id, i.SEGMENT AS Pistachio_segment,
(CASE when S.SUBSCRIPTION_TYPE = '5' then 'Y' else 'N' end ) PB_SUBS
(CASE WHEN S.SUBSCRIPTION_TYPE ='12' THEN 'Y' ELSE 'N' END) Daily_test,
(CASE when S.SUBSCRIPTION_TYPE ='8' then 'Y' else 'N' end) COOK_4_2
FROM IDEN_WITH_MAIL_ID i JOIN CUSTOMER_SUBSCRIPTION_FCT S
ON I.IDENTITY_ID = S.IDENTITY_ID and I.CUSTOMER_ID = S.CUSTOMER_ID
WHERE s.site_code ='PB' and s.subscription_end_date is null
Sounds like you need to group by customer_id and perform aggregations for the other columns you are selecting. For example:
sum(case when s.subscription_type = '5' then 1 else 0 end) as pb_subs_count
You could try one of two things:
Use a GROUP BY statement to combine all records with the same id, e.g.,
...
WHERE s.site_code ='PB' and s.subscription_end_date is null
GROUP BY i.customer_id
Use the DISTINCT command in your SELECT, e.g.,
SELECT DISTINCT i.customer_id, i.SEGMENT, ...
you could use a aggregation (SUM) on customer_id, but what do you expect to happen on the other fields? for example, if you have SUBSCRIPTION_TYPE 5 and 13 for the same customer (2 rows), which value do you want?
Perhaps you are looking for something like this:
SELECT i.customer_id, i.SEGMENT AS Pistachio_segment,
MAX(CASE when S.SUBSCRIPTION_TYPE = '5' then 'Y' else 'N' end ) PB_SUBS
MAX(CASE WHEN S.SUBSCRIPTION_TYPE ='12' THEN 'Y' ELSE 'N' END) Daily_test,
MAX(CASE when S.SUBSCRIPTION_TYPE ='8' then 'Y' else 'N' end) COOK_4_2
FROM IDEN_WITH_MAIL_ID i JOIN CUSTOMER_SUBSCRIPTION_FCT S
ON I.IDENTITY_ID = S.IDENTITY_ID and I.CUSTOMER_ID = S.CUSTOMER_ID
WHERE s.site_code ='PB' and s.subscription_end_date is null
GROUP BY i.customer_id, i.SEGMENT
I can't be sure, though, without knowing more about the tables involved.