I'm at my wits end attempting to make this happen. Currently I have 3 separate queries. For automation purposes I need to get the results of these three queries in one output, I cannot seem to join them properly to get the expected results.
QUERY 1:
SELECT OH.EXTN_HOST_ORDER_REF,
OL.EXTN_HOST_ORDER_LINE_REF,
OL.ORIGINAL_ORDERED_QTY,
OL.EXTN_TENDER_QUANTITY,
OL.EXTN_CUM_PICK_QTY,
OL.SHIPPED_QUANTITY,
OL.EXTN_REFUND_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OH.DOCUMENT_TYPE = '0001'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx'
ORDER BY PL.EXTN_HOST_ORDER_LINE_REF ASC;
QUERY 2:
SELECT RS.STATUS_QUANTITY AS RETURNED_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL,
YFS_ORDER_RELEASE_STATUS RS
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
AND RS.STATUS = '3700.02'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';
QUERY 3
SELECT RS.STATUS_QUANTITY AS CANCELLED_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL,
YFS_ORDER_RELEASE_STATUS RS
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
AND RS.STATUS = '9000'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';
The query should show NULL values where no data exists from query 2 & 3.
Thanks in advance for your help and advice!
If you are allowed to show both returned and cancelled quantities, the following simple edit should work. Hope this helps.
SELECT oh.extn_host_order_ref,
ol.extn_host_order_line_ref,
ol.original_ordered_qty,
ol.extn_tender_quantity,
ol.extn_cum_pick_qty,
ol.shipped_quantity,
ol.extn_refund_qty,
DECODE (rs.status, '3700.02', rs.status_quantity) AS returned_qty,
DECODE (rs.status, '9000', rs.status_quantity) AS cancelled_qty
FROM yfs_order_header oh
INNER JOIN yfs_order_line ol
ON oh.order_header_key = ol.order_header_key
LEFT OUTER JOIN yfs_order_release_status rs
ON ol.order_line_key = rs.order_line_key
WHERE oh.document_type = '0001' AND oh.extn_host_order_ref = 'xxxxxxxxxxx'
ORDER BY pl.extn_host_order_line_ref ASC;
Related
In the below query when i comment one line AND EVT.EVT_EVNT_ID(+) = DPT.DPT_EVNT_ID or add outer join (+) in one line AND EVT.EVT_ENTITY_ID ='G' the query will fetch results, else it will not give any results.
`
select * from
(select B11.ext_db_id,B11.FRST_LINE_OF_PRFRD_NAME, E11.*, D1.D1_evnt_id,
(SELECT E1.E1_DT
FROM S1core.E1_EVNT_DT_DTLS E1
WHERE E1.E1_ENTITY_ID = D1.D1_ENTITY_ID
AND E1.E1_EVNT_ID = D1.D1_EVNT_ID
AND E1.E1_D1_ID = D1.D1_D1_ID
AND E1.E1_DT_TYP = 'MEET'
AND E1.E1_OPTN_SEQ_N = '999'
AND E1.STATUS = 'AUTHD' ) "MEET_DATE"
FROM S1core.E1_EVNT_DT_DTLS E11, S1core.SECURITY_ACCOUNT SFA,
S1core.EXTRNL_SYS_DETAILS EXT,
S1core.D1_DPOT_EVNT_DTLS D1, S1core.IP_ACNT_RELATION i1,
S1core.ELG_ELGBLTY E1,S1core.P1_D1_PRXY_DTLS P1, S1core.EVT_DTLS EVT,
S1core.BUSINESS_PARTNER B1, S1core.BUSINESS_PARTNER B11
WHERE
P1.P1_EVNT_ID(+) = D1.D1_EVNT_ID
AND B1.BP_ID = i1.IP_ID
AND SFA.BP_ID = B11.BP_ID
AND B11.OWNER_ENTITY = SFA.OWNER_ENTITY
AND P1.P1_D1_ID(+) = D1.D1_D1_ID
AND P1.P1_ENTITY_ID(+) = D1.D1_ENTITY_ID
AND P1.STATUS(+) = 'AUTHD'
AND EXT.LVL_REF = SFA.SCA_REF
AND EXT.OWNER_ENTITY = SFA.owner_entity
AND EXT.EXTRNL_SYS_ID = '39'
AND EXT.BP_ID = SFA.BP_ID
AND EXT.LVL = 5
AND E1.ELG_SEC_ID = D1.D1_SEC_ID
AND D1.D1_ENTITY_ID = E1.ELG_ENTITY_ID
AND D1.D1_EVNT_ID = E1.ELG_EVNT_ID
AND D1.D1_D1_ID = E1.ELG_D1_ID
AND E1.ELG_ENTITY_ID = SFA.OWNER_ENTITY
AND E1.ELG_ACNT_ID = SFA.SCA_REF
AND i1.owner_entity = SFA.OWNER_ENTITY
AND i1.owner_entity = B1.OWNER_ENTITY
AND i1.SCA_REF = SFA.SCA_REF
AND i1.stat <> 2
AND EVT.EVT_ENTITY_ID ='G'
AND EVT.EVT_EVNT_ID(+) = D1.D1_EVNT_ID
AND EVT.STATUS(+) = 'AUTHD'
AND E11.E1_ENTITY_ID = D1.D1_ENTITY_ID
AND E11.E1_EVNT_ID = D1.D1_EVNT_ID
AND E11.E1_D1_ID = D1.D1_D1_ID
AND D1.D1_EVNT_GRP = 'MEETING'
AND E1.ELG_FNL_ELGBL_QNTTY > 0
AND D1.D1_ENTITY_ID = 'GSSIN' -- P_D1_ENTITY_ID
AND B11.ext_db_id LIKE 'LICMF' -- P_GLOBAL_CUSTODIAN
AND B1.ext_db_id LIKE '%' -- P_FUND_MANAGER
AND SUBSTR(EXT.LEVEL_EXTNL_SYS,1,5) LIKE '%' -- P_SUB_ACCOUNT_ID
AND SUBSTR(EXT.LEVEL_EXTNL_SYS,6,9) LIKE '%' -- P_SCHEMA_ID
AND SUBSTR( D1.D1_EVNT_TYP, 1, 4 ) LIKE '%' -- P_EVENT_TYPE
AND E11.E1_DT_TYP = 'MEET'
and D1.D1_evnt_id='12457886544'
AND D1.OU_ID IN('17','80') -- ('S1 INDIA PC')
) ;
Am I missing any outer join or the query is having any other issue as i can see that dpt_evnt_id='12457886544'is available in all tables.
`
I tried to run this query by commenting AND EVT.EVT_EVNT_ID(+) = DPT.DPT_EVNT_ID
and by adding outer join in EVT.EVT_ENTITY_ID (+) ='G'
You have:
AND EVT.EVT_ENTITY_ID ='G'
AND EVT.EVT_EVNT_ID(+) = D1.D1_EVNT_ID
AND EVT.STATUS(+) = 'AUTHD'
Even though the second two have (+) the first one does not so the join to EVT will be treated as an INNER JOIN and not an OUTER JOIN.
You should convert your code to use ANSI joins as it will be much easier to spot issues like this.
Note: There may be more issues like this. However, your code has so many joins, the syntax is archaic, and we have no way of validating the correctness that I stopped looking after finding the first issue. You are going to need to debug it all to check for any other issues.
Why does this code show invalid identifier when sum is used in distance and air_time column?
When sum is not used this statement process successfully but using sum I get error? I need to use sum for this statement.
MERGE INTO FACT_COMPANY_GROWTH F
USING (SELECT DISTINCT TIME_ID, FLIGHT_KEY, AEROPLANE_KEY, SUM(DISTANCE) AS TOTAL_DISTANCE, SUM(AIR_TIME) AS TOTAL_AIRTIME
FROM TRANSFORM_FLIGHT T
INNER JOIN TRANSFORM_AEROPLANE A
ON T.FK_AEROPLANE_KEY = A.AEROPLANE_KEY
INNER JOIN DIM_TIME D
ON D.YEAR = T.YEAR
AND D.MONTH = T.MONTH
GROUP BY TIME_ID, FLIGHT_KEY, AEROPLANE_KEY) S
ON (F.FK1_TIME_ID = S.TIME_ID
AND F.FK2_FLIGHT_KEY = S.FLIGHT_KEY
AND F.FK3_AEROPLANE_KEY = S.AEROPLANE_KEY
)
WHEN MATCHED THEN
UPDATE SET
F.TOTAL_AIRTIME = S.AIR_TIME,
F.TOTAL_DISTANCE = S.DISTANCE,
F.TOTAL_NO_OF_FLIGHTS = S.FLIGHT_KEY,
F.TOTAL_NO_OF_AEROPLANE = S.AEROPLANE_KEY
WHEN NOT MATCHED THEN
INSERT(FACT_ID, FK1_TIME_ID, FK2_FLIGHT_KEY, FK3_AEROPLANE_KEY, TOTAL_DISTANCE, TOTAL_AIRTIME, TOTAL_NO_OF_FLIGHTS, TOTAL_NO_OF_AEROPLANE)
VALUES
(NULL, S.TIME_ID, S.FLIGHT_KEY, S.AEROPLANE_KEY, S.DISTANCE, S.AIR_TIME, S.FLIGHT_KEY, S.AEROPLANE_KEY);
USING(
SELECT DISTINCT
TIME_ID,
FLIGHT_KEY,
AEROPLANE_KEY,
SUM(DISTANCE) AS TOTAL_DISTANCE,
SUM(AIR_TIME) AS TOTAL_AIRTIME
...) S
The problem is at UPDATE SET F.TOTAL_AIRTIME = S.AIR_TIME. There are 5 fields defined in S and none is named AIR_TIME.
UPDATE SET
F.TOTAL_AIRTIME = S.TOTAL_AIRTIME,
F.TOTAL_DISTANCE = S.TOTAL_DISTANCE,
Can some help me the best way convert the below Oracle code to Postgres
MERGE INTO INCLUSION_EXCLUSION_ALL A
USING (SELECT DISTINCT IEALL.rowid rid, IE.BLACKOUT_DURATION, ie.BLACKOUT_CATEGORY
FROM INCLUSION_EXCLUSION_ALL IEALL, INCLUSION_EXCLUSION_RULES IE
WHERE IE.PRI_PROMOTION_ID = IEALL.PRI_PROMOTION_ID
AND IE.PRI_OFFER_ID = IEALL.PRI_OFFER_ID
AND IE.SEC_PROMOTION_ID = IEALL.SEC_PROMOTION_ID
AND IE.SEC_OFFER_ID = IEALL.SEC_OFFER_ID
AND IE.BLACKOUT_DURATION > 0
AND (IEALL.BLACKOUT_DURATION <> IE.BLACKOUT_DURATION OR IEALL.BLACKOUT_CATEGORY <> IE.BLACKOUT_CATEGORY)
--AND IE.INCL_EXCL_IND <> 'I'
AND IE.SECONDARY_TYPE = 'B') B
ON (A.rowid = B.rid)
WHEN MATCHED THEN UPDATE SET A.BLACKOUT_DURATION = B.BLACKOUT_DURATION, A.BLACKOUT_CATEGORY = B.BLACKOUT_CATEGORY;
Thank You,
I want to know different ways on how we can do it as the rowid's are involved in here and are not compatible with Postgres esp in Joining using Alias even by using CTID,
Please advise
I am trying to use the following code :
SELECT a.part_no,
sum(a.BUY_QTY_DUE) AS Tot_Ord,
sum(b.REVISED_QTY_DUE) AS S_O_Tot
FROM CUSTOMER_ORDER_JOIN a
LEFT OUTER JOIN SHOP_ORD b ON a.part_no= b.part_no
AND b.contract = '20'
AND b.state = 'Planned'
WHERE a.PART_NO = '10002261'
AND a.OBJSTATE = 'Released'
AND a.CONTRACT = '10'
GROUP BY a.part_no
However I am seeing duplicated results, I know this is to do with the join but not sure how to fix :(
Results
The results I expect to see are,
Tot_Ord = 277
(6 instances of 23, 3 instances of 46, 1 instance of 1) 10 lines in total
S_O_Tot = 46
(2 instances of 23) 2 lines in Total
Any help would be greatly appreciated,
Thanks,
Jamie
SELECT a.part_no,
sum(a.BUY_QTY_DUE) AS Tot_Ord,
sum(nvl(b.REVISED_QTY_DUE,0)) AS S_O_Tot
FROM CUSTOMER_ORDER_JOIN a
LEFT OUTER JOIN SHOP_ORD b ON (a.part_no= b.part_no
AND b.contract = '20'
AND b.state = 'Planned')
WHERE a.PART_NO = '10002261'
AND a.OBJSTATE = 'Released'
AND a.CONTRACT = '10'
I have deleted grouping because you are filtering on this filed anyway
I am hoping someone can help me understand this XML Showplan for a long running query. This query comes from a Crystal Report developed by the software vendor that is very slow running, and I'm trying to convert it to an SSRS report while speeding the query up.
Here is a link to the Execution Plan - File hosted on File Dropper.
And here is the actual query itself:
Select Distinct
CD.desclong [Center],
CD.CenterType,
Sub.Description [SubCenter],
Org.Description [OrgCenter],
CO.CodeID [CenterC],
Sub.CodeID [SubCenterC],
Org.CodeID [OrgCenterC],
Reg.Description [Region],
Reg.CodeID [RegionC],
Rec.CodeID [RecID],
Rec.Description [RecName],
Rec.NickName [RecNickName],
Acct.AccountID [AcctCharCode],
DM.DriveID,
DM.ExternalID [DriveExtID],
DM.FromDateTime [DriveDate],
dbo.getShiftProductProjectionList(DSD.shiftid) [ProductProjections],
dbo.getShiftProcedureProjectionList(DSD.shiftid) [ProcedureProjections],
dbo.getShiftMobileList(DSD.shiftid, '1,2,3') [MobileSetups],
LD.LocationName [LocationName],
Case DM.OwnerType
When 1 Then FSM.DisplayName
When 0 Then Acct.InternalName
End [OwnerName], Acct.ExternalID [AcctExtID], SM.ExternalID [SiteExtID],
Stat.StatusText [DriveStatus],
DSD.ShiftID,
DSD.ShiftStart,
DSD.ShiftEnd,
DT.EarlyShiftStart,
DT.LateShiftEnd,
Changes.*,
dbo.getOriginalProcedureProjection(Changes.dsprocshiftid, Changes.dsprocprocedureid) [OriginalProjection],
(Select Top 1 DescMed From Production.[dbo].QuickCodes where CodeValue = 'ACTEXTIDTAG') [ActExtIDTag],
(Select Top 1 DescMed From Production.[dbo].QuickCodes where CodeValue = 'SITEEXTIDTAG') [SiteExtIDTag],
(Select Top 1 DescMed From Production.[dbo].QuickCodes where CodeValue = 'DRVEXTIDTAG') [DrvExtIDTag],
Case When (Changes.TableName = 'DriveShiftProcedureDetail') Then (Select Top 1 projectionid From driveshiftproceduredetail where uniquekey = Changes.owneruniquekey) else null end as ProjectionID
From
Production.[dbo].rpt_DriveMaster DM
Join Production.[dbo].rpt_DriveShiftDetail DSD on DSD.DriveID = DM.DriveID
Join Production.[dbo].CriticalDriveChanges Changes on (Changes.SourceDriveID = DM.DriveID and Changes.SourceShiftID = DSD.ShiftID and
Production.[dbo].returnDateTime(year(Changes.ChangeWhen),month(Changes.ChangeWhen),day(Changes.ChangeWhen)) between '01/04/2015' and '01/04/2015')
And
(
(
(Changes.TableName='DriveShiftProcedureDetail' and Changes.ColumnName='projection')
or
(Changes.TableName='DriveShiftDetail' and (Changes.ColumnName='ShiftStart' or Changes.ColumnName='ShiftEnd'))
or
(Changes.TableName='DriveShiftDetail' and Changes.ColumnName='StaffRequested')
or
(Changes.TableName='DriveShiftDetail' and Changes.changetype='Delete')
or
(Changes.TableName='DriveMaster' and Changes.ColumnName='StatusID' and Changes.NewValue='5')
or
(Changes.TableName='DriveMaster' and Changes.ColumnName='FromDateTime')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='Lead')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='TravelTo')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='Setup')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='Breakdown')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='TravelFrom')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='WrapUp')
)
and
(Changes.ChangeWho<>'RMADMIN')
)
Join Production.[dbo].rpt_DriveStatusDef Stat on Stat.StatusID = DM.StatusID
Join Production.[dbo].DriveTimes DT on DT.DriveID = DM.DriveID
Left Outer Join Production.[dbo].rpt_CenterDetail CD on CD.CenterID = DM.centerid
Left Outer Join Production.[dbo].IDViewRegion Reg on CD.Region = Reg.CodeID
Left Outer Join Production.[dbo].IDViewOrgCenter Org on CD.OrgCenter = Org.CodeID
Left Outer Join Production.[dbo].IDViewOrgSubCenter Sub on CD.OrgSubcenter = Sub.CodeID
Left Outer Join Production.[dbo].IDViewCollectionOp CO on cd.centerid = CO.CodeID
Left Outer Join Production.[dbo].IDViewRecruiter Rec on Rec.CodeID = DM.RecruiterID
Left Outer Join Production.[dbo].rpt_Accounts Acct on Acct.AccountID = DM.AccountID
Left Outer Join Production.[dbo].rpt_FixedSiteScheduleMaster FSM on FSM.DrawID = DM.DrawID
Left Outer Join Production.[dbo].rpt_LocationDetail LD on LD.LocationID = DM.LocationID
Join Production.[dbo].rpt_SiteMaster SM on SM.SiteID = DM.SiteID
Where
(dbo.returnDateTime(year(DM.Fromdatetime),month(DM.Fromdatetime),day(DM.Fromdatetime)) between '01/04/2015' and '01/04/2015')
or (dbo.returnDateTime(year(Changes.previousdrivedate),month(Changes.previousdrivedate),day(Changes.previousdrivedate)) between '01/04/2015' and '01/04/2015')
Here is the actual execution plan using Set Statistics XML On
Execution Plan
Thanks!
I removed the functions for returnDateTime and used the following instead:
-- In the FROM statement
Join Hemasphere_Dev.[dbo].CriticalDriveChanges Changes on (Changes.SourceDriveID = DM.DriveID And Changes.SourceShiftID = DSD.ShiftID And Changes.ChangeWhen Between '01/01/2015' And '01/15/2015')
--Hemasphere_Dev.[dbo].returnDateTime(year(Changes.ChangeWhen),month(Changes.ChangeWhen),day(Changes.ChangeWhen)) between '01/01/2015' And '01/15/2015')
-- In the WHERE statement
DM.FromDateTime Between '01/01/2015' And '01/15/2015'
Or Changes.PreviousDriveDate Between '01/01/2015' and '01/15/2015'
--(dbo.returnDateTime(year(DM.FromDateTime),month(DM.FromDateTime),day(DM.FromDateTime)) Between '01/01/2015' And '01/15/2015')
--Or (dbo.returnDateTime(year(Changes.previousdrivedate),month(Changes.previousdrivedate),day(Changes.previousdrivedate)) Between '01/01/2015' And '01/15/2015')
The query actually ran slower this way.