Convert case statement to logical operator in the where clause - sql

Can you please help me convert this piece of code to logical operator and use 'like' not '=' cause i know it's not efficient to put case statement on the where clause. Tia! Here's the code.
Where
(#reftype = '0' or
(#refnum = case #reftype when 'BN' then refBN when 'C' then refC when 'O' then refO else 'n/a' end)
)

Replace
or #refnum = case #reftype when 'BN' then refBN when 'C' then refC when 'O' then refO else 'n/a' end)
with
or (#reftype = 'BN' AND #refnum = refBN)
or (#reftype = 'C' AND #refnum = refC)
or (#reftype = 'O' AND #refnum = refO)
or #refnum = 'n/a'

You can translate it to boolean logic in this way:
WHERE #reftype = '0 'OR #refnum = 'n/a'
OR (#reftype = 'BN' AND #refnum = refbn)
OR (#reftype = 'C' AND #refnum = refc)
OR (#reftype = 'O' AND #refnum = refo)
But note that CASE in the WHERE-clause is not inefficient per se.

Related

Missing parenthesis for case statement

While running the below SQL I'm getting an error:
Missing parenthesis
SELECT DISTINCT
RPH.transport_mode, PCP.air_export_ind, PCP.air_import_ind
FROM
RATE_PROFILE_HEADER RPH, PARTNER_CHARGE_PROFILE PCP
WHERE
PCP.charge_code = RPH.Charge_code
AND PCP.charge_calculation_method = 'R'
AND RPH.RP_RECORD_ID = PCP.CHARGE_RATEPROF_RECORD_ID
AND CASE '&psTransport_mode'
WHEN 'A'
THEN (DECODE(RPH.transport_mode, 'ALL', 'A', RPH.transport_mode) = 'A') AND (PCP.air_export_ind = 'Y' or PCP.air_import_ind = 'Y')
WHEN 'M'
THEN (DECODE(RPH.transport_mode, 'ALL', 'M', RPH.transport_mode) = 'M') AND (PCP.air_export_ind = 'Y' or PCP.air_import_ind = 'Y')
ELSE NULL
END
I want to pass multiple conditions in THEN for WHERE clause.
Should I have to use an IF statement?
How about not using a CASE statement at all. It seems like you just need a nested conditional:
Select
distinct
RPH.transport_mode,
PCP.air_export_ind,
PCP.air_import_ind
from
RATE_PROFILE_HEADER RPH,
PARTNER_CHARGE_PROFILE PCP
where
PCP.charge_code = RPH.Charge_code
AND PCP.charge_calculation_method = 'R'
AND RPH.RP_RECORD_ID = PCP.CHARGE_RATEPROF_RECORD_ID
AND
(
'&psTransport_mode' = 'A' AND ( Decode(RPH.transport_mode,'ALL','A',RPH.transport_mode) = 'A') AND (PCP.air_export_ind = 'Y' or PCP.air_import_ind = 'Y')
OR
'&psTransport_mode' = 'M' AND ( Decode(RPH.transport_mode,'ALL','M',RPH.transport_mode) = 'M') AND (PCP.air_export_ind = 'Y' or PCP.air_import_ind = 'Y')
)

How to use bitwise operator in existing sql query?

Here is my sql query. I have column name "ExpenseBucketCoverage" in claim table in which I am storing bitwise operators store multiple values in one column like below
MED_COPAY = 1, MED_DED= 10, MED_COINS = 100, RX_COPAY = 1, RX_DED= 10, RX_COINS = 100
I want to replace hard coded value like MED_COPAY, MED_COINS, MED_DED, RX_DED, RX_COINS & RX_COPAY in query by using ExpenseBucketCoverage column value. Can some one please tell me how can I do that?
Someone has suggested me below soultion
retrieve data from claim and left joining the first matched record in eligibility. And then add custom code to loop through the datarows to split the rows by covered expense bucket, and set the service category code in-memory column based on the ExpenseBucketCoverage value for the claim.
SELECT
e.categoryid,
c.servicetype,
'II' AS RepordType,
e.TPAId AS TPA_Id,
e.EmployerCode,
e.SubscriberId,
e.MemberID,
c.ServiceFrom,
c.ServiceTo,
CASE
WHEN e.categoryid IN( 'MED_DED', 'RX_DED' ) THEN
deductible
WHEN e.categoryid IN( 'MED_COINS', 'RX_COINS' ) THEN
isnull(coins,0)
WHEN e.categoryid IN( 'MED_COPAY', 'RX_COPAY' ) THEN
copay
ELSE 0
END AS ClaimAmount,
'' AS AccountTypeCode,
'1' ClaimsCrossoverAutoPay,
e.CategoryId,
CASE c.ServiceType
WHEN 'H' THEN
CASE e.PayeeIndicator
WHEN 'N' THEN '0'
WHEN 'Y' THEN '1'
END
WHEN 'P' THEN '0'
END AS PayProvider,
CASE c.ServiceType
WHEN 'H' THEN
CASE PayeeIndicator
WHEN 'N' THEN '0'
WHEN 'Y' THEN '1'
END
WHEN 'P' THEN '0'
END AS ReimbursementMethod,
CASE c.ServiceType
WHEN 'H' THEN c.Provider
WHEN 'P' THEN ''
END AS ProviderId,
'1' EnforceAccountEffectiveDates,
c.Id,
c.ClaimNumber + e.CategoryId as 'ExternalClaimNumber',
c.ProviderName,
c.CarrierId + ';' + c.SourceClaimNumber AS Notes
FROM Claim c
INNER JOIN Eligibility e ON e.TPAId = c.TPAId AND e.EIN = c.EIN AND
c.Processed = 'Y' AND e.FilterType = 'Eligibility'
AND c.TPAId='PrimePay'
AND (c.ServiceFrom >= e.BenefitEffectiveDate
AND c.ServiceFrom <=e.BenefitTermDate)
AND ( ( c.PayorID = c.PatientSSN
AND e.SubscriberSSN = c.PatientSSN
AND (c.EmployeeFirstName = c.PatientFirstName
AND c.EmployeeLastName = c.PatientLastName)
AND(e.MemberSSN = '' OR e.MemberSSN = NULL)
AND(e.MemberFirstName = '' OR e.MemberFirstName = NULL)
AND(e.MemberLastName = '' OR e.MemberLastName = NULL))
OR((c.PayorID != c.PatientSSN AND e.MemberSSN = c.PatientSSN
AND e.MemberFirstName = c.PatientFirstName
AND e.MemberLastName = c.PatientLastName)
OR(c.PayorID != c.PatientSSN AND e.MemberFirstName = c.PatientFirstName
AND e.MemberLastName= c.PatientLastName)))
AND (( c.Servicetype ='P'
AND e.CategoryID IN('RX_COINS','RX_COPAY', 'RX_DED' ))
OR ( c.Servicetype = 'H'
AND e.CategoryID IN( 'MED_COINS','MED_COPAY', 'MED_DED' )))

Sybase proc throws "Incorrect syntax near '='"

I have the following piece of a SELECT query inside a stored procedure i'm developing:
AND (
CASE S.SWITCH
WHEN 'A' THEN P.TEST = T.OPTION_1
WHEN 'C' THEN P.TEST = T.OPTION_1 + T.OPTION_2
WHEN 'G' THEN P.TEST = T.OPTION_3
WHEN 'N' THEN TRUE
ELSE FALSE
END
)
I'm getting an Incorrect syntax near '=' error. Why would it complain about the first equals sign? It's a Sybase server if anyone is interested.
You case comparison should be something like below, if you are testing P.TEST value based on S.SWITCH case.
AND (
P.TEST =
CASE
WHEN S.SWITCH = 'A' THEN T.OPTION_1
WHEN S.SWITCH = 'C' THEN T.OPTION_1 + T.OPTION_2
WHEN S.SWITCH = 'G' THEN T.OPTION_3
WHEN S.SWITCH = 'N' THEN TRUE
ELSE FALSE
END
)
If you are comparing based on P.TEST and S.SWITCH, you can do either of following
Blorgbeard already provided this answer
AND
(
(S.SWITCH = 'A' AND P.TEST = T.OPTION_1) OR
(S.SWITCH = 'C' AND T.OPTION_1 + T.OPTION_2) OR
(S.SWITCH = 'G' AND P.TEST = T.OPTION_3) OR
(S.SWITCH = 'N')
)
If you want to make case statement work for this, following could be a possible solution.
AND (
CASE 1 =
WHEN S.SWITCH = 'A' AND P.TEST = T.OPTION_1 THEN 1
WHEN S.SWITCH = 'C' AND P.TEST = T.OPTION_1 + T.OPTION_2 THEN 1
WHEN S.SWITCH = 'G' AND P.TEST = T.OPTION_3 THEN 1
WHEN S.SWITCH = 'N' THEN 1
ELSE 0
END
)
Boolean expressions don't work like that in SQL. You can reformulate your switch like this:
AND (
(S.SWITCH = 'A' AND P.TEST = T.OPTION_1) OR
(S.SWITCH = 'C' AND T.OPTION_1 + T.OPTION_2) OR
(S.SWITCH = 'G' AND P.TEST = T.OPTION_3) OR
(S.SWITCH = 'N')
)
I know this is more of a comment than an answer, but hopefully this will lead to an answer, and I need to do formatted code for this so...
I tried this in Mysql and it worked. Could you try something like this in Sybase and see what it returns? The point being, extract the part that failed and test it out and see if you can figure out exactly what is wrong. It may be that Sybase is pointing to that equal sign but something else is really what is confusing it.
set #miller:='C';
set #mtime:=2;
select CASE #miller
WHEN 'A' THEN #mtime = 1
WHEN 'C' THEN #mtime = 2
WHEN 'G' THEN #mtime = 3
WHEN 'N' THEN TRUE
ELSE FALSE
END
This returns a 1 since miller = 'C' takes it to check if mtime = 2, which is true, and that means a 1 or true in Mysql.
Could you try and isolate this bit of code like this in Sybase?

Concatenating multiple CASE statements into one alias

After some previous help on how to approach a problem I am having with some legacy code, it seems like the best approach for my issue is to concatenate case statements to return a value I can parse out in PHP.
I am trying to do something like this, but it is returning many rows, and eventually getting this error:
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
SELECT org.org_id,
org.org_name_1,
Datename(YEAR, member.enroll_date) AS enroll_year,
Max(CASE
WHEN board.member_from IS NULL THEN 0
ELSE 1
END) AS board_member,
CASE
WHEN ( org.delete_reason = 'OUT'
AND org.org_delete_flag = 'Y'
AND org.org_status_flag = 'C' ) THEN 'out_of_business|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues > 0 ) THEN 'insurance_member|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues = 0
AND member.status_flag IN( 'C', 'P' ) ) THEN 'insurance_product|'
ELSE ''
END + CASE
WHEN ( member.enroll_date IS NOT NULL
AND member.status_flag NOT IN( 'C', 'P' ) ) THEN 'member_since|'
ELSE ''
END + CASE
WHEN ( org.org_relationship_parent = 'Y'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 'subsidiary_member|'
ELSE ''
END + CASE
WHEN ( org.org_misc_data_9 = 'PAC' ) THEN 'pac|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'PART' ) THEN 'partner_member|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'FREE'
AND org.org_status_flag = 'P' ) THEN 'associate_member|'
ELSE ''
END
--ELSE 'non_member'
--END
AS org_status,
60 AS expires_in,
CASE
WHEN stat.dues_type = 'M' THEN
CASE
WHEN ( stat.termination_date IS NULL ) THEN ( stat.flat_dues )
ELSE 0
END
ELSE
CASE
WHEN ( member.payments = 0 ) THEN member.dues_billed_annual
ELSE member.payments
END
END AS dues_level,
CASE
WHEN ( org.affiliate_code = 'PCCE'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 1
ELSE 0
END AS pcce_membr,
-- '$'+CONVERT(VARCHAR,#dues) AS dues_level,
Ltrim(#product_level) AS product_level,
Ltrim(#involve_level) AS involvement_level
FROM organiz AS org
LEFT JOIN affilbil AS member
ON member.status_id = org.org_id
AND member.dues_category = 'MBR'
LEFT JOIN individu AS ind
ON ind.org_id = org.org_id
LEFT JOIN commembr AS board
ON board.status_id = ind.ind_id
AND board.committee_code = '5'
AND board.member_to IS NULL
LEFT JOIN statinsmorn AS stat
ON stat.status_id = org.org_id
AND stat.carrier = 'BS'
AND stat.planz = 'PCI'
WHERE org.org_id = #org_id
GROUP BY org.org_id,
org.org_name_1,
member.enroll_date,
org.delete_reason,
org.org_status_flag,
org.org_delete_flag,
stat.status_id,
stat.flat_dues,
stat.dues_type,
stat.termination_date,
org.org_misc_data_9,
org_relationship_parent,
org.dues_category,
member.status_flag,
member.dues_billed_annual,
member.payments,
stat.carrier,
org.Affiliate_Code
Well, this is embarrassing.
When I was making my changes to the stored procedure, I had inadvertently placed a call to the same procedure at the bottom. So I was recursively calling the same procedure over and over again. DOH.

How to do a case of 2 parameters in a sql query

In a query I do :
SUM(CASE kbpres.soort WHEN 'K' THEN kbpres.prijs END)
AS FACKOSTEN
but I would like to use something like WHEN 'K' or WHEN 'V' THEN
but how do I do that ??
CASE WHEN kbpres.soort = 'K' OR kbpres.soort = 'V' THEN kbpres.prijs ELSE NULL END
Or
CASE WHEN kbpres.soort IN ('K', 'V') THEN kbpres.prijs ELSE NULL END
Or
CASE kbpres.soort WHEN 'K' THEN kbpres.prijs
WHEN 'V' THEN kbpres.prijs
ELSE NULL
END
Or
CASE WHEN kbpres.soort = 'K' THEN kbpres.prijs
WHEN kbpres.soort = 'V' THEN kbpres.prijs
ELSE NULL
END
SUM(CASE kbpres.soort WHEN 'K' THEN kbpres.prijs
WHEN 'V' THEN kbpres.prijs
END)
AS FACKOSTEN
OR
SUM(CASE WHEN kbpres.soort IN('K', 'V') THEN kbpres.prijs END)
AS FACKOSTEN
Try this:
SUM(CASE WHEN kbpres.soort IN ('K', 'V') THEN kbpres.prijs END)
AS FACKOSTEN
We don't know wich database engine you use, but in MySQL you can write this :
SUM(CASE WHEN kbpres.soort = 'K' OR kbpres.soort = 'V' THEN kbpres.prijs END)
AS FACKOSTEN