Pseducode Requirement - sql

I require Pseducode for the below mentioned code. I am not able to understand this code. Please help me out it. Thanks in advance.
select E.Pan,E.AST_YR
from prd_ods_itb.ast_tt_elgble_cases E
where E.Faceless_Flag = 'Y'
and E.Jur_To_Faceless_Conv_Flag is null
and 0 = (select count(1)
from prd_ods_itb.com_tt_notices_details D, prd_ods_itb.com_tt_send_email S
where S.Notice_Id = D.seq_no
and D.Section_Code = '142(1)'
and D.Mode_Of_Selection = 'S'
and D.Document_Number <> 'DRAFT'
and D.Status = '3'
and S.Comm_Type_Flag = 'O'
and D.Bpm_Request_Id = E.bpm_request_id
and exists (select 1
from prd_ods_itb.com_tt_send_email n
where n.notice_id = s.notice_id
and n.comm_type_flag = 'I'))
and 0 <> (select count(1)
from prd_ods_itb.com_tt_notices_details D
where D.Section_Code = '142(1)'
and D.Mode_Of_Selection = 'S'
and D.Document_Number <> 'DRAFT'
and D.Status = '3'
and D.Bpm_Request_Id = E.bpm_request_id)
and E.assessment_status <>'C'

Related

Avoid calling a scalar function multiple times in a MERGE and UPDATE statement

I want to call function that return scalar multiple times.
How can I do that in a MERGE statement?
My code:
MERGE [dbo].MyTable m
USING (select * from Table2 edbl
where IsRowError = 0) edbTable
on edbTable.Filed1 = m.Filed1
WHEN MATCHED THEN UPDATE SET
m.Filed2 = case when dbo.func1(edbTable.Filed7) = 1 then 1
when dbo.func1(edbTable.Filed7) = 0 then 1
else 0 end,
m.Filed3 = case when dbo.func1(edbTable.Filed7) = 0 then 0 end,
m.Filed4 = case when dbo.func1(edbTable.Filed7) = 0 then 3 end,
m.Filed5 = case when dbo.func1(edbTable.Filed7) = 0 then 9 else 5 end,
m.Filed6 = getdate();
I want also to do it in UPDATE statement:
UPDATE md
set Filed1 = case when dbo.func1(edbl.Filed7) = 0 then 0 end,
Filed5 = case when dbo.func1(edbTable.Filed7)=0 then 9 else 5 end,
Filed6 = getdate()
from Table2 edbl
join Table3 m on edbl.Filed2= m.Filed2
join Table4 md on m.Filed3=md.Filed3
join Table5 mb on md.Filed4 = mb.Filed4
where IsRowError = 0
You can do it in the subquery:
MERGE [dbo].MyTable m
USING (select edbl.*, dbo.func1(edbTable.Filed7) as func1
from Table2 edbl
where IsRowError = 0
) edbTable
on edbTable.Filed1 = m.Filed1
WHEN MATCHED THEN UPDATE SET
m.Filed2 = (case when func1 = 1 then 1
when func1 = 0 then 1
else 0
end),
m.Filed3 = case when func1 = 0 then 0 end,
m.Filed4 = case when func1 = 0 then 3 end,
m.Filed5 = case when func1 = 0 then 9 else 5 end,
m.Filed6 = getdate();
However, it is not clear that this is a big win. If most rows do not match, this might not be a big improvement.
You could do it once inside USING part:
MERGE [dbo].MyTable m
USING (select *, dbo.func1(Filed7) AS f from Table2 edbl
where IsRowError = 0) edbTable
on edbTable.Filed1 = m.Filed1
WHEN MATCHED THEN UPDATE SET
m.Filed2 = case when f = 1 then 1
when f = 0 then 1
else 0 end,
m.Filed3 = case when f = 0 then 0 end,
m.Filed4 = case when f = 0 then 3 end,
m.Filed5 = case when f = 0 then 9 else 5 end,
m.Filed6 = getdate();
You can use APPLY with UPDATE statement :
UPDATE md
SET Filed1 = CASE WHEN fun = 0 THEN 0 END,
Filed5 = CASE WHEN fun = 0 THEN 9 ELSE 5 END,
Filed6 = getdate()
FROM Table2 edbl JOIN
Table3 m
ON edbl.Filed2 = m.Filed2 JOIN
Table4 md
ON m.Filed3 = md.Filed3 JOIN
Table5 mb
ON md.Filed4 = mb.Filed4 CROSS APPLY
( VALUES (dbo.func1(edbl.Filed7))
) edbl1(fun)
where IsRowError = 0;

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)

SQL Query - How to suppress repeating values in the result set?

I'm trying to suppress the repeating values in TotalCarton column. Have tried to replace the value either blank or null but went failed. Any help?
Here is the SQL Script:
SELECT ORDERS.StorerKey,
ORDERS.OrderKey,
PackKey = (SELECT MAX(PackKey) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE ORderKey = ORDERS.OrderKey),
PackHU = BAX_PACK_DTL.OuterPackID,
SalesOrderNum = ( SELECT Upper(Max(ORDERDETAIL.CustShipInst01)) FROM ORDERDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey),
DeliveryNum = Upper(ORDERS.ExternOrderKey),
TotalCarton = ( CASE BAX_PACK_DTL.PackType WHEN 'C' THEN Count(DISTINCT(BAX_PACK_DTL.OuterPackID))
ELSE 0 END ),
TotalPallet = ( CASE BAX_PACK_DTL.PackType WHEN 'P' THEN Count(DISTINCT(BAX_PACK_DTL.OuterPackID))
ELSE 0 END ),
SumCarton = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE PackType = 'C' AND PackKey = '0000000211'),
SumPallet = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE PackType = 'P' AND PackKey = '0000000211'),
AddWho = Upper(ORDERS.EditWho),
ORDERS.AddDate
FROM ORDERS WITH (NOLOCK) INNER JOIN ORDERDETAIL WITH (NOLOCK) ON ORDERS.StorerKey = ORDERDETAIL.StorerKey
AND ORDERS.OrderKey = ORDERDETAIL.OrderKey
INNER JOIN PICKDETAIL WITH (NOLOCK) ON ORDERDETAIL.StorerKey = PICKDETAIL.StorerKey
AND ORDERDETAIL.OrderKey = PICKDETAIL.OrderKey
AND ORDERDETAIL.OrderLineNumber = PICKDETAIL.OrderLineNumber
INNER JOIN BAX_PACK_DTL WITH (NOLOCK) ON PICKDETAIL.OrderKey = BAX_PACK_DTL.OrderKey
AND PICKDETAIL.PickDetailKey = BAX_PACK_DTL.PickDetailKey
WHERE (SELECT COUNT(DISTINCT(ORDERKEY)) FROM PICKDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey ) > 0
AND BAX_PACK_DTL.PackKey = '0000000211'
AND BAX_PACK_DTL.OuterPackID IN
('P111111111',
'P22222222',
'P33333333')
GROUP BY ORDERS.StorerKey,
ORDERS.OrderKey,
ORDERS.ExternOrderKey,
ORDERS.HAWB,
ORDERS.SO,
ORDERS.EditWho,
ORDERS.AddDate,
PICKDETAIL.WaveKey,
BAX_PACK_DTL.OuterPackID,
BAX_PACK_DTL.PackKey,
BAX_PACK_DTL.PackType
ORDER BY BAX_PACK_DTL.OuterPackID ASC
Below is the current result set based on the query above.
Your code looks really strange. I would expect the query to use conditional aggregation and look more like this:
SELECT ORDERS.StorerKey, ORDERS.OrderKey,
PackKey = (SELECT MAX(PackKey) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE ORderKey = ORDERS.OrderKey),
PackHU = BAX_PACK_DTL.OuterPackID,
SalesOrderNum = ( SELECT Upper(Max(ORDERDETAIL.CustShipInst01)) FROM ORDERDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey),
DeliveryNum = Upper(ORDERS.ExternOrderKey),
TotalCarton = COUNT(DISTINCT CASE BAX_PACK_DTL.PackType WHEN 'C' THEN BAX_PACK_DTL.OuterPackID END),
TotalPallet = COUNT(DISTINCT CASE BAX_PACK_DTL.PackType WHEN 'P' THEN BAX_PACK_DTL.OuterPackID END),
SumCarton = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL bpd WHERE pbd.PackType = 'C' AND pbd.PackKey = '0000000211'),
SumPallet = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL bpd WHERE pbd.PackType = 'P' AND pbd.PackKey = '0000000211'),
AddWho = Upper(ORDERS.EditWho),
ORDERS.AddDate
FROM . . .
GROUP BY ORDERS.StorerKey, ORDERS.OrderKey, Upper(ORDERS.ExternOrderKey),
Upper(ORDERS.EditWho), ORDERS.AddDate;
This may not be exact. You have not qualified column names, given the table structure, and are using very arcane query syntax, mixing subqueries and aggregations. But it should give an idea.

several select counts in select could be enhanced?

I have the below query , If the table data grows and contained millions of record , would it cause performance issue ? can it be enhanced in a way I dont run select count() in the select several time ? I already added index on the 6 columns.
I know the join is bad I am already enhancing the query
SELECT
P.CID,
P.CB,
(SELECT COUNT(1)
FROM EED_TABLE F
WHERE F.ID = P.ID
AND F.BC = P.BC
AND F.PCD = P.PCD
AND F.CID = P.CID
AND F.BRC = P.BRC
AND F.ST = 'F') ST_F,
(SELECT COUNT(1)
FROM EED_TABLE N
WHERE N.ID = P.ID
AND N.BC = P.BC
AND N.PCD = P.PCD
AND N.COMP_CODE = P.COMP_CODE
AND N.BRC = P.BRC
AND N.ST = 'N') ST_N,
(SELECT COUNT(1)
FROM EED_TABLE A
WHERE A.ID = P.ID
AND A.BC = P.BC
AND A.PCD = P.PCD
AND A.CID = P.CID
AND F.BRC = P.BRC
AND A.ST = 'A') ST_A
FROM EED_TABLE P ,EDD_DEF D
WHERE P.PCD = D.PCD
AND P.BC= D.BC
AND P.CD= 1
AND P.ID = 1
AND P.BC= 22
AND P.PCD = 31
I always prefer to go for JOIN instead of having more select statements, this is how I would do it, not knowing your data of course so maybe it won't work, but give it a shot and let me know of results and speed
select
p.cid, p.cb, SUM(case when f.id is not null then 1 else 0 end) st_f,
, SUM(case when a.id is not null then 1 else 0 end) st_a
, SUM(case when n.id is not null then 1 else 0 end) st_n
from eed_table p
join edd_def d on p.pcd = d.pcd and p.bc = d.bc
left join eed_table f on f.id = p.id and f.bc = p.bc
and f.pcd = p.pcd and f.cid = p.cid
and f.brc = p.brc and f.st = 'f'
left join eed_table a on p.id = a.id and p.bc = a.bc
and p.pcd = a.pcd and p.cid = a.cid
and p.brc = a.brc and a.st = 'a'
left join eed_table n on n.id = p.id and n.bc = p.bc
and n.pcd = p.pcd and n.comp_code = p.comp_code
and n.brc = p.brc and n.st = 'n'
where p.cd = 1 and p.id = 1 and p.bc = 22 and p.pcd = 31
group by p.cid, p.cb
Seems that your query can be rewritten with window functions as:
SELECT
DISTINCT P.CID, P.CB
, SUM(CASE WHEN P.ST = 'F' THEN 1 ELSE 0 END) OVER (PARTITION BY P.ID, P.BC, P.PCD, P.CID, P.BRC) ST_F
, SUM(CASE WHEN P.ST = 'N' THEN 1 ELSE 0 END) OVER (PARTITION BY P.ID, P.BC, P.PCD, P.COMP_CODE, P.BRC) ST_N
, SUM(CASE WHEN P.ST = 'A' THEN 1 ELSE 0 END) OVER (PARTITION BY P.ID, P.BC, P.PCD, P.CID, P.BRC) ST_A
FROM
EED_TABLE P
WHERE
P.CD = 1
AND P.ID = 1
AND P.BC = 22
AND P.PCD = 31
AND EXISTS (SELECT 1 FROM EDD_DEF D WHERE P.PCD = D.PCD AND P.BC= D.BC)

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