Data issues on SQL - 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)

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.

Pseducode Requirement

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'

ORA-01719: outer join operator (+) not allowed in operand of OR or IN

While running my query I'm getting an error:
ORA-01719: outer join operator (+) not allowed in operand of OR or IN
01719. 00000 - "outer join operator (+) not allowed in operand of OR or IN"
Cause: An outer join appears in an or clause.
Action: If A and B are predicates, to get the effect of (A(+) or B),
try (select where (A(+) and not B)) union all (select where (B))
I'm getting this error due to my AND Or conditions under where clause which i have defined under last.
SELECT DISTINCT rp.rp_record_id,
rp.rate_profile_name,
rph.rate_type,
rph.charge_code,
rph.transport_mode,
rph.place_of_receipt_code,
rph.origin_type,
rph.destination_type
FROM rate_profile rp,
rate_profile_header rph,
partner_charge_profile pcp,
partner,
commodity_codes com,
offices,
partner_relationships pr,
company_partners cp,
employee e
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 = pr.partner_id(+)
AND pr.employee_no = e.employee_no
AND partner.partner_id = cp.partner_id
AND cp.company_id = '&CompanyID'
AND cp.company_id = pcp.company_id
AND cp.company_id(+) = pr.company_id
AND pcp.charge_code = rph.charge_code
AND pcp.charge_calculation_method = 'R'
AND rph.rp_record_id = pcp.charge_rateprof_record_id
AND ( ('&psRate_type') = 'SEL'
AND ( rph.rate_type = 'SEL'
AND pcp.charge_calculation_method = 'R'
AND rph.rp_record_id = pcp.charge_rateprof_record_id))
OR ( ('&psRate_type') = 'BUY'
AND ( rph.rate_type = 'BUY'
AND pcp.cost_calculation_method = 'R'
AND rph.rp_record_id = pcp.cost_rateprof_record_id))
OR ( ('&psRate_type') = 'All'
AND ( ( rph.rate_type = 'SEL'
AND pcp.charge_calculation_method = 'R'
AND rph.rp_record_id = pcp.cost_rateprof_record_id)
OR ( (rph.rate_type = 'BUY')
AND pcp.cost_calculation_method = 'R'
AND rph.rp_record_id = pcp.cost_rateprof_record_id)))
Please suggest
The OR condition referred to in the outer join error message is in the '&psRate_type' filter section, which I don't think you intended to be part of the join. If you bracket all of those conditions inside an AND predicate, the error will go away:
select distinct
rp.rp_record_id
, rp.rate_profile_name
, rph.rate_type
, rph.charge_code
, rph.transport_mode
, rph.place_of_receipt_code
, rph.origin_type
, rph.destination_type
from rate_profile rp
, rate_profile_header rph
, partner_charge_profile pcp
, partner
, commodity_codes com
, offices
, partner_relationships pr
, company_partners cp
, employee e
where rp.rp_record_id = rph.rp_record_id
and pcp.partner_id = partner.partner_id
and com.cc_record_id (+) = rph.commcode_record_id
and pcp.company_id = offices.company_id
and offices.office_type = 'T'
and pr.partner_id (+) = partner.partner_id
and pr.employee_no = e.employee_no
and partner.partner_id = cp.partner_id
and cp.company_id = '&CompanyID'
and cp.company_id = pcp.company_id
and cp.company_id (+) = pr.company_id
and pcp.charge_code = rph.charge_code
and pcp.charge_calculation_method = 'R'
and rph.rp_record_id = pcp.charge_rateprof_record_id
-- Added brackets below:
and ( ('&psRate_type' = 'SEL' and rph.rate_type = 'SEL' and pcp.charge_calculation_method = 'R' and rph.rp_record_id = pcp.charge_rateprof_record_id)
or ('&psRate_type' = 'BUY' and rph.rate_type = 'BUY' and pcp.cost_calculation_method = 'R' and rph.rp_record_id = pcp.cost_rateprof_record_id)
or ('&psRate_type' = 'All'
and ( (rph.rate_type = 'SEL' and pcp.charge_calculation_method = 'R' and rph.rp_record_id = pcp.cost_rateprof_record_id)
or (rph.rate_type = 'BUY' and pcp.cost_calculation_method = 'R' and rph.rp_record_id = pcp.cost_rateprof_record_id))
)
);
However, company_partners can't actually be an outer join, because cp.company_id = '&CompanyID'. The same thing goes for partner_relationships, because pr.employee_no = e.employee_no. The only real outer join is to commodity_codes, which is not used in the query anyway and can be removed without affecting the result.
With this taken into account, I get the following as the ANSI version:
select distinct
rp.rp_record_id
, rp.rate_profile_name
, rph.rate_type
, rph.charge_code
, rph.transport_mode
, rph.place_of_receipt_code
, rph.origin_type
, rph.destination_type
from company_partners cp
join partner_relationships pr
on pr.company_id = cp.company_id
and pr.partner_id = cp.partner_id
/*join partner -- not needed if pr.partner_id is a FK to partner
on partner.partner_id = pr.partner_id*/
join partner_charge_profile pcp
on pcp.company_id = cp.company_id
and pcp.partner_id = cp.partner_id
join rate_profile_header rph
on rph.charge_code = pcp.charge_code
and rph.rp_record_id = pcp.charge_rateprof_record_id
join rate_profile rp
on rp.rp_record_id = rph.rp_record_id
join employee e
on e.employee_no = pr.employee_no
join offices
on offices.company_id = pcp.company_id
/*left join commodity_codes com -- not used
on com.cc_record_id = rph.commcode_record_id*/
where cp.company_id = '&CompanyID'
and pcp.charge_calculation_method = 'R'
and offices.office_type = 'T'
and ( ('&psRate_type' = 'SEL' and rph.rate_type = 'SEL' and pcp.charge_calculation_method = 'R')
or ('&psRate_type' = 'BUY' and rph.rate_type = 'BUY' and pcp.cost_calculation_method = 'R' and rph.rp_record_id = pcp.cost_rateprof_record_id)
or ('&psRate_type' = 'All'
and ( (rph.rate_type = 'SEL' and pcp.charge_calculation_method = 'R' and rph.rp_record_id = pcp.cost_rateprof_record_id)
or (rph.rate_type = 'BUY' and pcp.cost_calculation_method = 'R' and rph.rp_record_id = pcp.cost_rateprof_record_id)))
);

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

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.