IF ROWS IS ALL EQUAL - sql

I want to use CASE WHEN Command, if my column IsApproved is all equal to 1 then the display is 'COMPLETE' then if there's still 0 then it is pending, depending on the ResignTxn number. How can I do that?
For example, if there's still 0 in txn number 45, then the output must be
pending, then if all the value is 1 then it must be complete.

Try below query:
select ResignTxn,
-- it counts 0 in particular ResignTxn
case when sum(case when isApproved = 0 then 1 else 0 end) > 0 then 'pending' else 'complete' end
from MyTable
group by ResignTxn

SELECT CASE WHEN MIN(IsApproved) =0 THEN 'Pending' ELSE 'Complete' END AS Status
FROM [Table]
Group by ResignTxn

Check min value
select ResignTxn , case min(IsApproved) when 1 then `COMPLETE' else 'pending' end
from mytable
group by ResignTxn

I personally would leave the logic out of the DB, just get the relevant data and evaluate it in your application, but you can do it like this if you want:
SELECT COUNT(*) AS countResult
WHERE IsApproved=0,
CASE
WHEN countResult > 0 THEN "PENDING"
ELSE "COMPLETE"
END AS ResultText
FROM myTable;

You may use:
select case when IsApproved = 1 then 'Complete' else 'Pending' end as Status
from YOURTABLE

Related

How to display combined result with if statement from multiple rows?

I have below table (screenshot) in database which records approval status as T1_APPROVAL for defects opened on specific date with APP_NAME.
I want to display only 1 row per APP_NAME for specific date.
Eg. if all T1_APPROVAL='Y' where APP_NAME='APP-B' it should display Approved, else Pending.
This is what I roughly prepared so far, but doesn't give the intended result.
SELECT DISTINCT
CASE WHEN t1_approval <> 'Y' THEN 'Pending' ELSE 'Approved' END
AS status
FROM DAV_CR_DEFECT_DPLOYMENT_STATUS
WHERE TRUNC (deploy_date) = TO_DATE ('24-JAN-2018', 'dd-mon-yyyy')
GROUP BY (t1_approval);
If you look at the screenshot, APP-A has one entry as N, and 1 entry as Y -- which should display as PENDING; and for APP-B all T1_APPROVAL is Y -- which should appear as APPROVED.
This is how my end result, should display like:
APP_NAME | T1_APPROVAL
======================
APP-A | PENDING
APP-B | APPROVED
SELECT APP_NAME,
CASE
WHEN AVG(ASCII(T1_APPROVAL)) = ASCII('Y')
THEN 'APPROVED'
ELSE 'PENDING'
END AS T1_APPROVAL
FROM DAV_CR_DEFECT_DPLOYMENT_STATUS
WHERE TRUNC(deploy_date) = TO_DATE('24-JAN-2018', 'dd-mon-yyyy')
GROUP BY APP_NAME
Try that... it's been a long day, and I'm probably forgetting something stupid.
*Edit: forgot to put the date selection clause in.
Take counts of T1_APPROVAL = N for every APP_NAME in an inner query and if it is 0 => PENDING else APPROVED.
SELECT A.DEPLOY_DATE, A.APP_NAME,
CASE WHEN A.PENDING_ENTRIES = 0 THEN 'APPROVED' ELSE 'PENDING' END AS T1_APPROVAL
FROM
(SELECT TRUNC(DEPLOY_DATE) AS DEPLOY_DATE, APP_NAME,
SUM(CASE WHEN T1_APPROVAL = 'N' THEN 1 ELSE 0 END) AS PENDING_ENTRIES
FROM DAV_CR_DEFECT_DPLOYMENT_STATUS
GROUP BY TRUNC(DEPLOY_DATE), APP_NAME) A;
You want a group by and some conditional logic:
SELECT TRUNC(deploy_date), app_name,
(CASE WHEN MIN(TL_APPROVAL) = 'Y' THEN 'Approved' ELSE 'Pending' END) as status
FROM DAV_CR_DEFECT_DPLOYMENT_STATUS
GROUP BY TRUNC(deploy_date), app_name;
You can use MIN too,
SELECT APP_NAME,
DECODE(MIN(T1_APPROVAL), 'Y', 'APPROVED', 'PENDING')
FROM DAV_CR_DEFECT_DPLOYMENT_STATUS
WHERE TRUNC(deploy_date) = TO_DATE('24-JAN-2018', 'dd-mon-yyyy')
GROUP BY APP_NAME

Why doesn't the "having" clause work?

For the following query:
select count(distinct email_address)
from
(
select distinct email_address,
case when elq_activity_type='EmailSend' then 1 else 0 end 'Sends',
case when elq_activity_type='Bounceback' then 1 else 0 end 'Bounces',
case when elq_activity_type='EmailOpen' then 1 else 0 end 'Opens',
case when elq_activity_type='EmailClickthrough' then 1 else 0 end 'Clicks'
from elq_stg_activity
) a
having sum(sends-bounces)>0
The having clause doesn't seem to be doing anything. What am I doing wrong?
Need to get all unique emails that had an email delivered to them (send-bounce).
Thanks!
I think you want this:
select count(email_address)
from (select email_address,
sum(case when elq_activity_type = 'EmailSend' then 1 else 0 end) Sends,
sum(case when elq_activity_type = 'Bounceback' then 1 else 0 end) as Bounces,
sum(case when elq_activity_type = 'EmailOpen' then 1 else 0 end) as Opens,
sum(case when elq_activity_type = 'EmailClickthrough' then 1 else 0 end) as Clicks
from elq_stg_activity
group by email_address
) a
where sends = bounces;
There are numerous issues with your query. This is the only sensible interpretation I could think of.

Avoiding a divide by zero error in a case statement

I am getting a divide by zero error in my script.
Can anyone please help.
I am trying to divide two records and one of them has zero in it. I dont want to lose the row, please advise.
select DATEPART(Year,Request_date) as "Year",
DATEPART(Month,Request_date) as "Month",
COUNT([MONTH_OF_SUSPENSION]) as "Request" ,
sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end) as "Paid in 24hrs",
COUNT([MONTH_OF_SUSPENSION])/sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end) as "Achieved"
FROM suspension_br
where REQUEST_STATUS = 'OTHERS'
GROUP BY DATEPART(Year,Request_date),DATEPART(Month,Request_date)
You can introduce a second case to check the result of the sum:
case
when sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end) > 0
then COUNT([MONTH_OF_SUSPENSION])/sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end)
else 0 /* a default value that makes sense to you */
end as "Achieved"
Looking at your code I could assume you are using MSSQL and therefore you could use nullif which returns null if two arguments are equal. So for example your code could look like :
COUNT([MONTH_OF_SUSPENSION])/nullif(sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end),0) as "Achieved"
What it does is if the value of the sum operator is equal 0 then the divisor is turn from zero into null and that will result in the entire equation to become null.
use another case statement to check the result of your sum
select DATEPART(Year,Request_date) as "Year",
DATEPART(Month,Request_date) as "Month",
COUNT([MONTH_OF_SUSPENSION]) as "Request" ,
sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) as "Paid in 24hrs",
case
when sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) = 0 then 'whatever you want in this case'
else COUNT([MONTH_OF_SUSPENSION])/sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) as "Achieved"
FROM suspension_br
where REQUEST_STATUS = 'OTHERS'
GROUP BY DATEPART(Year,Request_date),DATEPART(Month,Request_date)
although this is pretty nasty looking, so you could tidy it up a bit with a sub-select:
select
year,
month,
request,
PaidIn24hrs,
case
when PaidIn24hrs = 0 then 'whatever you want in this case'
else request/PaidIn24hrs
end as "Achieved"
from
(
select DATEPART(Year,Request_date) as "Year",
DATEPART(Month,Request_date) as "Month",
COUNT([MONTH_OF_SUSPENSION]) as "Request" ,
sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) as "PaidIn24hrs"
FROM suspension_br
where REQUEST_STATUS = 'OTHERS'
GROUP BY DATEPART(Year,Request_date),DATEPART(Month,Request_date)
)

Counting specific cells in SQL Server

I have a table like this:
The RP Type column specifies the type of rate plan which can be (low, high, normal)
I want to create a view in which I can see number of each subscribers' which can be high, normal, low.
It should be like this I guess:
Create view t as
select
SubID,
Count(something) as NumeOfHIGH,
Count(Something) as NumOfLOW,
Count(Something) as NumOfNormal
Group by SubID
I might be wrong..Thank you
You can form your query in the following way:
SELECT SubID,
SUM (
CASE
WHEN RP_Type='High' THEN 1
ELSE 0 END
) AS NumOfHigh,
SUM (
CASE
WHEN RP_Type='Low' THEN 1
ELSE 0 END
) AS NumOfLow,
SUM (
CASE
WHEN RP_Type='Normal' THEN 1
ELSE 0 END
) AS NumOfNormal
FROM
<table_name>
Group by SubID
If there are multiple RP_Type in each of High, Low and Normal Category, you can include each type with WHEN in respective category.
For more information and reference: Conditional Processing using CASE
i tried this :
select SubscriberID
,COUNT(*) AS NumOfPlans
,SUM([SP active days]) as ActiveDays
,SUM(Case when [RP Type]='low' then 1 else 0 end ) as #LowPlan
,SUM(Case when [RP Type]='high' then 1 else 0 end ) as #NormalPlan
,SUM(Case when [RP Type]='high' then 1 else 0 end ) as #HighPlan
,SUM(Case when [RP Type]='promotion' then 1 else 0 end ) as #PromotionsPlan
,SUM(Case when [RP Type]='portal' then 1 else 0 end ) as #PortablePlan
,SUM(Case when [RP Type]='newyear' then 1 else 0 end ) as #NewYearPlan
,SUM(Case when [RP Type]='hamaval1' then 1 else 0 end ) as #HamrahAval1Plan
,SUM(Case when [RP Type]='hamaval2' then 1 else 0 end ) as #HamrahAval2Plan
,SUM(Case when [RP Type]='samsung' then 1 else 0 end ) as #SamsungPlan
,SUM(Case when [RP Type]='DEMO' then 1 else 0 end ) as #DemoPlan
,SUM(Case when [RP Type]IS null then 1 else 0 end ) as #NuLL
,SUM([Extra Quota]) as SumGIG
from [Cus Consumption].[dbo].CustData
where [Expiry Date]<= '2014 - 01 - 15'
group by [SubscriberID]
The ironic thing is that i does not return a correct answer..I don't know why #Rohit Aggrawal
is this helpful?
Create view t as
select From [tableName]
[Rp Type],count(*) as NumberOfSubscribers
Group by [Rp Type]
This gives you Number of subscribers in each type, and type
Hope this helpful

Odd GROUP BY output DB2 - Results not as expected

If I run the following query:
select load_cyc_num
, crnt_dnlq_age_cde
, sum(cc_min_pymt_amt) as min_pymt
, sum(ec_tot_bal) as budget
, case when ec_tot_bal > 0 then 'Y' else 'N' end as budget
, case when ac_stat_cde in ('A0P','A1P','ARP','A3P') then 'Y' else 'N' end as arngmnt
, sum(sn_close_bal) as st_bal
from statements
where (sn_close_bal > 0 or ec_tot_bal > 0)
and load_cyc_num in (200911)
group by load_cyc_num
, crnt_dnlq_age_cde
, case when ec_tot_bal > 0 then 'Y' else 'N' end
, case when ac_stat_cde in ('A0P','A1P','ARP','A3P') then 'Y' else 'N' end
then I get the correct "BUDGET" grouping, but not the correct "ARRANGEMENT" grouping, only two rows have a "Y".
If I change the order of the case statements in the GROUP BY, then I get the correct grouping (full Y-N breakdown for both columns).
Am I missing something obvious?
Try moving
, sum(cc_min_pymt_amt) as min_pymt, sum(ec_tot_bal) as budget
to the end of the select statement, i.e.
select load_cyc_num,
crnt_dnlq_age_cde,
case when ec_tot_bal > 0 then 'Y' else 'N' end as budget,
case when ac_stat_cde in ('A0P','A1P','ARP','A3P') then 'Y' else 'N' end as arngmnt,
sum(sn_close_bal) as st_bal,
sum(cc_min_pymt_amt) as min_pymt,
sum(ec_tot_bal) as budget
from statements
where (sn_close_bal > 0 or ec_tot_bal > 0)and load_cyc_num in (200911)
group by load_cyc_num,
crnt_dnlq_age_cde,
case when ec_tot_bal > 0 then 'Y' else 'N' end ,
case when ac_stat_cde in ('A0P','A1P','ARP','A3P') then 'Y' else 'N' end