Missing parenthesis for case statement - sql

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')
)

Related

Why isn't this CTE recognized?

I'm trying to pull in student majors to this sql by using a CTE, but when I try to add the CTE fields, or join the CTE with an implicit join, which works fine in other queries, oracle throws the error 'invalid identifier'. Any thoughts?
This is only the first part of many unions in this sql but I've seen examples where a CTE works fine with unions so I don't think thats it, and besides that when I run this code without the unions I get the same errors, 'invalid identifier'.
with major (pidm, major) as
(
select
a.sgbstdn_pidm,
a.sgbstdn_majr_code_1
from
sgbstdn a
where a.sgbstdn_term_code_eff = (select
max(b.sgbstdn_term_code_eff)
from
sgbstdn b
where
a.sgbstdn_pidm = b.sgbstdn_pidm
and b.sgbstdn_term_code_eff <= '202004'
and b.sgbstdn_term_code_eff > '202001')
)
select --authorized aid
spriden_id id
,spriden_last_name ||', '||spriden_first_name || ' '|| spriden_mi "Name"
,major.major "Major"
,rpratrm_fund_code "Fund"
,rpratrm_period "Period"
,rpratrm_offer_amt "Offer"
,rpratrm_accept_amt "Accept"
,null "Loan Net Orig Amt"
,RPRATRM_AUTHORIZE_AMT "Authorized"
,rpratrm_paid_amt "Paid"
,c.hr "Census Hours"
,r.hr "Enrolled Hours"
,c.con "Consortium"
,b.pbcode "P Budget Code"
,b.pbcode_locked "P Budget Code Locked?"
,b.b_locked "Budget Locked"
--,astd.astd "Academic Standing"
,s.sap "SAP Code"
,s.term "SAP Term"
,decode(h.pidm, null, 'No', 'Yes') "Holds"
,admit.admit "Admitted?"
from
spriden
,rpratrm
--,(select SGVSTDN_pidm pidm, sgvstdn_astd_desc astd from SGVSTDN where SGVSTDN_term_code = '202003') astd
--admitted?
,(select
sgbstdn_pidm pidm
,case
when sgbstdn_levl_code like 'N%' then 'No'
when sgbstdn_levl_code is null then 'No Student Record found this term'
else 'Yes'
end admit
from
sgbstdn
where
sgbstdn_term_code_eff = '202003') admit
--HOLDS
,(select
rorhold_pidm pidm
from
rorhold
where
to_char(sysdate, 'YYYYMMDD') <= to_char(RORHOLD_TO_DATE, 'YYYYMMDD')
and to_char(sysdate, 'YYYYMMDD') >= to_char(RORHOLD_FROM_DATE, 'YYYYMMDD')
) h
--SAP
,(select
a.rorsapr_pidm pidm
,a.rorsapr_term_code term
,a.rorsapr_sapr_code sap
from
rorsapr a
where
a.rorsapr_term_code = (select max(b.rorsapr_term_code) from rorsapr b where a.rorsapr_pidm = b.rorsapr_pidm and b.rorsapr_term_code <= '202003')) s
--Period Budget Code Lock/FREEZE
,(select
rbrapbg_pidm pidm
,RBRAPBG_PBGP_CODE pbcode
,decode(RBRAPBG_PBGP_CODE_LOCK_IND, 'Y', 'Yes', 'No') pbcode_locked
,decode(RBRAPBG_BUDGET_FREEZE_IND, 'Y', 'Yes', 'No') b_locked
from
rbrapbg
where
RBRAPBG_RUN_NAME = 'ACTUAL'
and RBRAPBG_PERIOD = '202003'
and RBRAPBG_AIDY_CODE = '2021') b
,(select
rorenrl_pidm pidm
,rorenrl_term_code term
,RORENRL_FINAID_ADJ_HR hr
,RORENRL_CONSORTIUM_IND con
from
rorenrl
where
rorenrl_enrr_code = 'STANDARD'
and rorenrl_term_code like '202003') c
,(select
sfrstcr_pidm pidm
,sfrstcr_term_code term
,sum(sfrstcr_credit_hr) hr
from
sfrstcr
where
sfrstcr_term_code like '202003'
and sfrstcr_rsts_code in (select stvrsts_code from stvrsts where STVRSTS_INCL_SECT_ENRL = 'Y')
group by sfrstcr_pidm, sfrstcr_term_code) r
where
spriden_change_ind is null
and spriden_pidm = rpratrm_pidm
and rpratrm_aidy_code = '2021'
and RPRATRM_AUTHORIZE_AMT is not null
and rpratrm_pidm = c.pidm(+)
and rpratrm_period = c.term(+)
and rpratrm_pidm = r.pidm(+)
and rpratrm_period = r.term(+)
and rpratrm_period = '202003'
and rpratrm_pidm = b.pidm(+)
and rpratrm_pidm = s.pidm(+)
and rpratrm_pidm = h.pidm(+)
and rpratrm_pidm = admit.pidm(+)
and rpratrm_pidm = major.pidm
--and rpratrm_pidm = astd.pidm(+)
and not exists (select 'asdf' from SGVSTDN a where a.sgvstdn_pidm = rpratrm_period and a.SGVSTDN_term_code = (select max(b.sgvstdn_term_code) from sgvstdn b where b.sgvstdn_term_code <= '202003' and b.sgvstdn_astd_desc is not null and a.sgvstdn_pidm = b.sgvstdn_pidm) and a.sgvstdn_astd_code = 'SU')
and (r.hr is not null or c.hr is not null)
and not exists (select 'afd' from rorstat where rorstat_pidm = rpratrm_pidm and rorstat_aidy_code = rpratrm_aidy_code and RORSTAT_DISB_REQ_COMP_DATE is null)
and not exists (select 'afd' from rrrareq where rrrareq_pidm = rpratrm_pidm and rrrareq_aidy_code = rpratrm_aidy_code and rrrareq_fund_code = rpratrm_fund_code and RRRAREQ_SAT_IND = 'N')
--this will exclude those without loans should be a loan only query. if chur is auth but mpn not done, student won't show up
and exists (select 'asdf' from rlrdlor, rlrdldb where RLRDLOR_PIDM = rlrdldb_pidm and RLRDLOR_PIDM = rpratrm_pidm and RLRDLOR_LOAN_NO = rlrdldb_loan_no and RLRDLOR_AIDY_CODE = rlrdldb_aidy_code
and rlrdlor_aidy_code = rpratrm_aidy_code and RLRDLOR_FUND_CODE = rlrdldb_fund_code and rlrdlor_fund_code = rpratrm_fund_code and RLRDLOR_MPN_LINKED_IND = 'Y')
Adding the CTE to the from clause made it work. Forgot about that little detail.

How can i remove an group result sql

I have a SQL query but it gives me more results that I want. For example when I run the query I will get 20 results from 2 products. Each product has a group by and the group by of the g.linkedline must be ignored and not shown in the results.
This is the query that I have
I tried to make a select over my select. I also tried a TOP 1 but it gives me 1 result.
SELECT i.itemcode,
g.warehouse,
Sum(g.aantal) AS Voorraad,
Max(CASE
WHEN g.transtype = 'N' THEN g.sysmodified
ELSE NULL
END) AS LastDate
FROM dbo.gbkmut AS g
INNER JOIN dbo.items AS i
ON g.artcode = i.itemcode
AND g.reknr = i.glaccountdistribution
AND ( g.transtype = 'N'
OR ( g.transtype = 'B'
AND g.transsubtype = 'B'
AND g.freefield1 NOT IN ( 'B', 'Q', 'W', 'K' )
AND g.blockitem = '0'
AND g.allocationtype = 'F' )
OR ( g.transtype = 'B'
AND g.transsubtype = 'B'
AND g.freefield1 = 'V'
AND g.allocationtype = 'B' ) )
WHERE g.transtype = 'N'
AND ( i.itemcode = 'TESTARTIKEL'
OR i.itemcode = 'IQ7-60-2-INT' )
GROUP BY i.itemcode,
g.warehouse,
g.linkedline
I'd try it like this, you can use the window function row_number to force a ranking. If you are interested in only 1 thing per unique item code or where house, the nyou substitute the thing of interest in the Partition by clause and then specify what you want to order on in the order by clause. For example in the query below the item of interest is the ItemCode and we are ordering on the warehouse and aantal desc (which I'm assuming you want to see items with more aantal than less, but you can remove desc if you that suits your needs.
select * from
(
SELECT row_number()over(partition by i.Itemcode order by g.warehouse, g.aantal desc) as rowno, i.ItemCode, g.warehouse, SUM(g.aantal) AS Voorraad, MAX(CASE WHEN g.transtype = 'N' THEN g.sysmodified ELSE NULL END) AS LastDate
FROM dbo.gbkmut AS g INNER JOIN dbo.Items AS i
ON g.artcode = i.ItemCode AND g.reknr = i.GLAccountDistribution AND (
g.transtype = 'N' OR
(g.transtype = 'B' AND
g.transsubtype = 'B' AND
g.freefield1 NOT IN ('B', 'Q', 'W', 'K') AND
g.BlockItem = '0' AND
g.AllocationType = 'F') OR ( g.transtype = 'B' AND g.transsubtype = 'B' AND g.freefield1 = 'V' AND g.AllocationType = 'B'))
WHERE g.transtype = 'N' AND (i.ItemCode = 'TESTARTIKEL' OR i.ItemCode = 'IQ7-60-2-INT') GROUP BY i.ItemCode, g.warehouse, g.LinkedLine
) x where rowno=1;
SELECT i.ItemCode, g.warehouse, SUM(g.aantal) AS Voorraad,
MAX(CASE WHEN g.transtype = 'N' THEN g.sysmodified ELSE NULL END) AS LastDate
FROM dbo.gbkmut AS g INNER JOIN dbo.Items AS i
ON g.artcode = i.ItemCode AND g.reknr = i.GLAccountDistribution AND (
g.transtype = 'N' OR
(g.transtype = 'B' AND
g.transsubtype = 'B' AND
g.freefield1 NOT IN ('B', 'Q', 'W', 'K') AND
g.BlockItem = '0' AND
g.AllocationType = 'F') OR ( g.transtype = 'B' AND g.transsubtype = 'B' AND g.freefield1 = 'V' AND g.AllocationType = 'B'))
WHERE g.transtype = 'N' AND i.ItemCode = 'IQ7-60-2-INT' GROUP BY i.ItemCode, g.warehouse, g.LinkedLine
This returns results first Voorraad 0 and with the script of DwightReynoldson it returns 1080
I found the problem. thank you all!
There was a desc to the Voorraad thats is why the result wasn't correct.
This is the Code
select * from
(
SELECT
i.ItemCode,
g.warehouse,
SUM(g.aantal) AS Voorraad,
MAX(CASE
WHEN g.transtype = 'N' THEN g.sysmodified
ELSE NULL
END) AS LastDate,
row_number()
over
(partition by
i.Itemcode
order by
g.warehouse
) as rowno
FROM dbo.gbkmut AS g
INNER JOIN dbo.Items AS i
ON g.artcode = i.ItemCode
AND g.reknr = i.GLAccountDistribution
AND ( g.transtype = 'N'
OR ( g.transtype = 'B'
AND g.transsubtype = 'B'
AND g.freefield1 NOT IN ( 'B', 'Q', 'W', 'K' )
AND g.blockitem = '0'
AND g.allocationtype = 'F' )
OR ( g.transtype = 'B'
AND g.transsubtype = 'B'
AND g.freefield1 = 'V'
AND g.allocationtype = 'B' ) )
WHERE g.transtype = 'N'
AND ( i.itemcode = 'TESTARTIKEL'
OR i.itemcode = 'IQ7-60-2-INT' )
GROUP BY i.itemcode,
g.warehouse,
g.linkedline
) x where rowno=1 ;
You can use:
select *****Here the columns that you want to use****** from (****Your query****) anyName
In your case:
select *****Here the columns that you want to use****** from (
SELECT i.ItemCode, g.warehouse, SUM(g.aantal) AS Voorraad, MAX(CASE WHEN g.transtype = 'N' THEN g.sysmodified ELSE NULL END) AS LastDate
FROM dbo.gbkmut AS g INNER JOIN dbo.Items AS i
ON g.artcode = i.ItemCode AND g.reknr = i.GLAccountDistribution AND (
g.transtype = 'N' OR
(g.transtype = 'B' AND
g.transsubtype = 'B' AND
g.freefield1 NOT IN ('B', 'Q', 'W', 'K') AND
g.BlockItem = '0' AND
g.AllocationType = 'F') OR ( g.transtype = 'B' AND g.transsubtype = 'B' AND g.freefield1 = 'V' AND g.AllocationType = 'B'))
WHERE g.transtype = 'N' AND (i.ItemCode = 'TESTARTIKEL' OR i.ItemCode = 'IQ7-60-2-INT') GROUP BY i.ItemCode, g.warehouse, g.LinkedLine
) Name

Data issues on SQL

I'm not able to fetch data only for the BUY parameter. SEL parameter is working fine. May be an issue with joins.
SELECT DISTINCT
RP.RP_RECORD_ID,
RP.RATE_PROFILE_NAME,
RPH.RATE_TYPE,
RPH.CHARGE_CODE,
RPH.TRANSPORT_MODE,
RPH.PLACE_OF_RECEIPT_CODE,
(SELECT CD_DESCRIPTION FROM CODE_DETAIL
WHERE CM_CODE ='145' AND CD_CODE = RPH.ORIGIN_TYPE),
DECODE(RPH.ORIGIN_TYPE, 'PO', RPH.ORIGIN_PORT_CODE, 'CNT' , ORIGIN_COUNTRY_CODE, 'A', ORIGIN_AREA_CODE, 'CIT', CITY_ORIGIN, 'PV', RPH.ORIGIN_REGION_CODE) ORIGIN,
RPH.PLACE_OF_DELIVERY_CODE,
RPH.CARRIER_CODE,
PCP.PARTNER_ID,
PARTNER.PARTNER_NAME,
COM.COMMODITY_DESCRIPTION,
RPH.REMARK, RPH.RPH_RECORD_ID,
RPH.ORIGIN_TYPE, RPH.DESTINATION_TYPE,
(SELECT DISTINCT EMP.employee_name
FROM partner PAR, employee EMP, partner_relationships PRE
WHERE PARTNER.partner_id = PAR.partner_id
AND PAR.partner_id = PRE.partner_id
AND PRE.EMPLOYEE_NO = EMP.EMPLOYEE_NO
AND PRE.transport_mode = RPH.transport_mode
AND (PRE.IMPORT_EXPORT_IND IS NULL OR -- ' Start of New code by SB on 16-FEB-2010 for WHD136784
PRE.IMPORT_EXPORT_IND = (CASE
WHEN (RPH.City_Origin = 'VNSGN' OR rph.origin_country_code ='VN' OR rph.origin_port_code ='VN' OR RPH.origin_port_code = 'VNSGN')
THEN 'E'
WHEN (RPH.City_Dest = 'VNSGN' OR RPH.dest_country_code = 'VN' OR RPH.dest_port_code = 'VNSGN' OR RPH.dest_port_code = 'VNSGN')
THEN 'I'
ELSE ''
END)) -- 'End of New code by SB on 16-FEB-2010 for WHD136784
AND PRE.company_id = CP.company_id
AND rownum = 1) salesman, -- 'End of WHD136784 new code
RPH.RPH_RECORD_ID, RPH.CURRENCY_CODE
FROM
RATE_PROFILE RP, RATE_PROFILE_HEADER RPH,
PARTNER_CHARGE_PROFILE PCP, PARTNER ,
COMMODITY_CODES com, OFFICES,
company_partners CP
WHERE
RP.RP_RECORD_ID = RPH.RP_RECORD_ID
AND PCP.PARTNER_ID = PARTNER.PARTNER_ID
AND RPH.COMMCODE_RECORD_ID = COM.CC_RECORD_ID(+)
AND PCP.COMPANY_ID = OFFICES.COMPANY_ID
AND OFFICES.OFFICE_TYPE = 'T'
AND partner.partner_id = CP.partner_id
AND CP.COMPANY_ID = '{?CompanyID}'
AND CP.company_id = PCP.company_id
AND PCP.charge_code = RPH.Charge_code
AND (('&psRate_type') = 'SEL' AND PCP.charge_calculation_method = 'R' AND RPH.RP_RECORD_ID = PCP.CHARGE_RATEPROF_RECORD_ID
OR
('&psRate_type') = 'BUY' AND PCP.cost_calculation_method = 'R' AND RPH.RP_RECORD_ID = PCP.COST_RATEPROF_RECORD_ID)
)
Data is coming for the 'SEL' parameter but for 'BUY' it is fetching 0 records.
I checked the joins everything but unable to figured out what's the problem.
No issue with AND OR condition under where clause.
Please suggest
I have resolved this issue using below joins conditions:
(RPH.RP_RECORD_ID = PCP.CHARGE_RATEPROF_RECORD_ID OR RPH.RP_RECORD_ID = PCP.COST_RATEPROF_RECORD_ID)

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' )))

Convert case statement to logical operator in the where clause

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.