Why left outer join works like inner join on ABAP CDS view and a custom table? - abap

I have created an ABAP CDS view using ACDOCA, BSID, MARA, MAKT and such tables.
Now my CDS view contains list of customers along with their outstanding (FI) documents.
Against these customers, FI documents and certain more parameters, I am maintaining certain data in a custom table.
Now using left outer join I want to connect this CDS view with custom table.
The problem now is that this join is working like inner join, so if my custom table is blank my output is displaying without values whereas it should be displaying CDS view as it is, and custom table fields as blank.
Now based on similar issues on web I did research and found some useful points:
all conditions on right table are included at most in ON join condition
tried to include null values from right side custom table as well.
tried using just one parameter on join condition to see the result (still working as inner join)
Here is my complete select statement, where:
ZPD1 is my ABAP CDS view of ACDOCA, BSDI ... tables
ZPD_HSL is an ABAP CDS view for customer wise GL amount calculation.
ZFI_PDRATE_MAP is my custom table which contains rate for customer / FI docs combination.
What could be a misstep in this statement?
SELECT
FROM ZPD1( PBUKRS = #BUKRS-LOW ,
POSTFROM = #COL_DATE-LOW ,
POSTTO = #COL_DATE-HIGH ) AS A
LEFT OUTER JOIN ZFI_PDRATE_MAP AS F
ON A~PROD_CATEGORY = F~PROD_CATEGORY
AND A~OD_DATE GE F~POST_DT_FROM
AND A~OD_DATE LE F~POST_DT_TO
AND F~COL_DT_FROM GE #COL_DATE-LOW
AND F~COL_DT_TO LE #COL_DATE-HIGH
AND F~SALES_OFFICE = #VKBUR-LOW
AND CASE WHEN A~ARREAR_DAYS <= 0 THEN 'NOD'
WHEN A~ARREAR_DAYS > 0 THEN 'OD'
END = F~OD
INNER JOIN KNA1 AS H
ON H~KUNNR = A~KUNNR
INNER JOIN TVKBT AS I
ON I~VKBUR = F~SALES_OFFICE
AND I~SPRAS = #SY-LANGU
LEFT OUTER JOIN SKAT AS J
ON J~SAKNR = A~HKONT
AND J~SPRAS = 'E'
AND J~KTOPL = '1000'
LEFT OUTER JOIN MAKT AS K
ON K~MATNR = A~MATNR
AND K~SPRAS = 'E'
LEFT OUTER JOIN T001W AS L
ON L~WERKS = A~WERKS
FIELDS
A~RBUKRS AS BUKRS,
A~KUNNR,
H~NAME1,
F~SALES_OFFICE,
I~BEZEI,
A~BELNR AS COL_BELNR,
A~GJAHR AS COL_GJAHR,
A~HSL AS COL_AMT,
A~BUDAT AS COL_DATE,
A~KEY_DATE AS KEY_OS_DATE,
A~OD_DOC AS OS_BELNR,
A~OD_YR AS OS_GJAHR,
A~BLART ,
A~OD_DATE AS OS_DATE,
CASE WHEN A~SHKZG = 'H' THEN CAST( A~WRBTR * -1 AS CURR( 12, 2 ) )
ELSE A~WRBTR
END AS OS_AMT ,
A~DUE_DATE,
A~ARREAR_DAYS,
CASE WHEN A~ARREAR_DAYS <= 0 THEN 'NOD'
WHEN A~ARREAR_DAYS > 0 THEN 'OD'
ELSE ' '
END AS OD_IND,
A~HKONT,
J~TXT50,
A~PROD_CATEGORY,
A~MATNR,
K~MAKTX,
A~WERKS ,
L~NAME1,
A~BWTAR,
A~PROFIT_CENTRE AS PRCTR,
COALESCE( F~PD_RATE , 0 ) AS RATE,
0 AS PD_AMT,
CASE WHEN A~ARREAR_DAYS > 0 THEN
CAST(
DIVISION(
( CAST( A~WRBTR * F~PD_RATE AS CURR( 15 ,2 ) ) ) ,
100 ,
3 )
AS CURR( 15, 2 ) )
* -1
ELSE
DIVISION(
( CAST( A~WRBTR * F~PD_RATE AS CURR( 15 , 2 ) ) ) ,
100 ,
3 )
END AS ITEM_PD_AMT, "DISCOUNT_RATE,
0 AS ITEM_PD_AMT1,
A~MAIN_TYPE
WHERE A~KUNNR IN #KUNNR
AND NOT EXISTS (
SELECT *
FROM ZFI_PAYMENT_DISC
WHERE BELNR = A~BELNR
AND GJAHR = A~GJAHR
AND BUKRS = A~RBUKRS
AND SALES_OFFICE = #VKBUR-LOW
AND A~KUNNR IN #KUNNR
AND OS_BELNR EQ A~OD_DOC )
AND A~ARREAR_DAYS <= #OD
AND ( F~COL_AMT_FROM LE (
SELECT SUM( COLLECT )
FROM ZPD_HSL( BUKRS = #BUKRS-LOW ,
BUDATFROM = #COL_DATE-LOW ,
BUDATO = #COL_DATE-HIGH )
WHERE KUNNR = A~KUNNR )
AND F~COL_AMT_TO GE (
SELECT SUM( COLLECT )
FROM ZPD_HSL( BUKRS = #BUKRS-LOW ,
BUDATFROM = #COL_DATE-LOW ,
BUDATO = #COL_DATE-HIGH )
WHERE KUNNR = A~KUNNR )
)
ORDER BY A~KUNNR, A~BELNR ,A~DUE_DATE ASCENDING
INTO TABLE #PROV_POST.

This is a mere guess, as your complex setup makes it hard to reproduce a working example.
My assumption is that the INNER JOIN TVKBT AS I ON I~VKBUR = F~SALES_OFFICE is getting in the way. It inner-joins to your custom table, aliased as F, and might lead to an overreduction of records if the join order is interpreted wrong.
I'd recommend to remove that join and see whether that fixes the basic join result.
In any way, I would recommend to add parentheses and reorder the join clauses to make clear what the "left" side of the equation is, and to remove any ambiguity, both for the interpreter, and the reader.

Related

Remove rows of data based on data in the combination of two columns

The code below produces the left block of data
It gives me the entire subset however I only need to see: For the same SATNR-VKORG combination where ALL Variant_Status is I2. If any of the Variant_Status is NOT I2, then do not show the entire SATNR-VKORG combination. My final output should only be the first two lines in the data below whereas all the other rows do not qualify.
I can't figure out how to do this but I'm thinking to use a count function like the right block to concat the SATNR-VKORG and SATNR-VKORG-Variant_Status and do a count of each unique combination. For the same SATNR-VKORG combination, if the two counts are identical then that means display else do not show. Even that I don't know how to code it, anyone help or have any better idea?
SELECT TOP (1000)
MARA.MATNR,
MARA.SATNR,
MARA.ATTYP,
MARA.MTART,
MARA.MSTAE,
MARA.LVORM,
MVKE.VMSTA as Variant_Status,
MVKE.VTWEG, mvke.VKORG, MVKE2.
VMSTA as Generic_Status,
MVKE2.VTWEG, MVKE2.VKORG,
mara.satnr + mvke.vkorg as concated
from [dgSAP_PRD].dbo.MARA AS MARA
JOIN [dgSAP_PRD].dbo.MVKE AS MVKE ON MARA.MATNR = MVKE.MATNR
JOIN [dgSAP_PRD].dbo.MARA AS MARA2 ON MARA.SATNR = MARA2.MATNR
JOIN [dgSAP_PRD].dbo.MVKE AS MVKE2 ON MARA2.MATNR = MVKE2.MATNR
WHERE MARA.MTART != 'ZODE'
AND MARA.ATTYP in (02)
AND MARA.LVORM = ''
AND MVKE2.VTWEG = '34'
AND MVKE.VTWEG = '34'
AND MVKE.VKORG=MVKE2.VKORG
and mvke2.vmsta != 'I2'
ORDER BY MARA.SATNR,MVKE.VKORG,MVKE2.VKORG, MARA.MATNR
WITH cte AS
(
SELECT TOP (1000)
MARA.MATNR,
MARA.SATNR,
MARA.ATTYP,
MARA.MTART,
MARA.MSTAE,
MARA.LVORM,
MVKE.VMSTA as Variant_Status,
MVKE.VTWEG, mvke.VKORG, MVKE2.
VMSTA as Generic_Status,
MVKE2.VTWEG, MVKE2.VKORG,
mara.satnr + mvke.vkorg as concated,
SUM(CASE WHEN Variant_Status <> 'I2' THEN 1 ELSE 0 END) OVER (PARTITION BY SATNR, VKORG) marker
from [dgSAP_PRD].dbo.MARA AS MARA
JOIN [dgSAP_PRD].dbo.MVKE AS MVKE ON MARA.MATNR = MVKE.MATNR
JOIN [dgSAP_PRD].dbo.MARA AS MARA2 ON MARA.SATNR = MARA2.MATNR
JOIN [dgSAP_PRD].dbo.MVKE AS MVKE2 ON MARA2.MATNR = MVKE2.MATNR
WHERE MARA.MTART <> 'ZODE'
AND MARA.ATTYP in (02)
AND MARA.LVORM = ''
AND MVKE2.VTWEG = '34'
AND MVKE.VTWEG = '34'
AND MVKE.VKORG=MVKE2.VKORG
and mvke2.vmsta <> 'I2'
)
SELECT *
FROM cte
WHERE marker = 0
ORDER BY SATNR, VKORG, VKORG, MATNR

PIVOT on multiple columns SQL server (Aspen Relay Database)

I'm using a vendor supplied Relay Database (Aspen), which is running on MS SQL server). I'm attempting to write a pivot query that needs to pivot on 2 columns.
I created a temp table since the data is across multiple tables.
WITH TEMP_TABLE AS (
SELECT
R.LOCATIONID LLOCATIONID, R.ID RID, s.groupname SGROUPNAME,t.settingname TSETTINGNAME, s.setting SSETTING
from tsetting1 s
inner join tsettype1 t on t.relaytype=s.relaytype and t.groupname = s.groupname and t.rownumber = s.rownumber
INNER JOIN TREQUEST Q ON S.REQUESTID = Q.ID
INNER JOIN TRELAY R ON R.ID = Q.RELAYID
INNER JOIN TLOCATION L ON L.ID = R.LOCATIONID
where s.requestid=29117
)
select * from TEMP_TABLE
That select all from Temp returns 38 rows of data, a subset is shown here:
RID -----SGROUPNAME------TSETTINGNAME-------SSETTING
31297 LOAD1 ENABLE TRUE
31297 LOAD1 ANGLE 60
31297 LOAD2 CALCULATED_LOAD 12269
ETC....
I added this pivot, which gets me close:
PIVOT (MAX(SSETTING) FOR TSETTINGNAME IN (ENABLE, REACH, ANGLE, CALCULATED_LOADABILITY, ZLE, CTR, PTR, KVNOM, PICKUP, PERCENTAGE)) P
Returned result from Pivot:
RID-----SGROUPNAME-----ENABLE----REACH----ANGLE----CALCULATED_LOADABILITY
31297 LOAD1 TRUE 15 60 9444
31297 LOAD2 TRUE 10 30 12269
31297 LOAD3 TRUE 20 60 14167
ETC...
I would like to have the data as 1 record for RID 31297, where LOAD1-ENABLE, LOAD2-ENABLE, LOAD3-ENABLE, LOAD1-REACH, ETC. are all headers.
I've tried multiple pivots and cross apply, but I can't seem to get the data to display correctly.
Let me know if anything is unclear or if you need more information. Any help will be greatly appreciated.
Thanks,
Joe C.
It may be easier with a bunch of case statements and a group by RID. By the way this is the "original" method of pivoting before PIVOT was implemented.
select RID
, Load1_Enable = MAX(case when SGROUPNAME = 'Load1' then enable else null end)
, Load2_Enable = MAX(case when SGROUPNAME = 'Load2' then enable else null end)
from [YourTable]
group by RID
I would go all the way to the first cte though:
WITH TEMP_TABLE AS (
SELECT
R.LOCATIONID LLOCATIONID, R.ID RID, s.groupname SGROUPNAME,t.settingname TSETTINGNAME, s.setting SSETTING
from tsetting1 s
inner join tsettype1 t on t.relaytype=s.relaytype and t.groupname = s.groupname and t.rownumber = s.rownumber
INNER JOIN TREQUEST Q ON S.REQUESTID = Q.ID
INNER JOIN TRELAY R ON R.ID = Q.RELAYID
INNER JOIN TLOCATION L ON L.ID = R.LOCATIONID
where s.requestid=29117
)
select RID
,Load1_Enable = MAX(case when SGROUPNAME = 'Load1' and TSETTINGNAME = 'Enable' then SSETTING else null end)
from TEMP_TABLE
group by RID
Note MAX is only there for an aggregate -- you should be aggregating only one record.

Two select statements turn into one

I have set of two queries. In first query, if is separate from second, I got good results.
First query
SELECT *
FROM
(
SELECT nks.[Id]
, nks.[IdNarudzbe]
, nks.[IdArtikla] as artikal
, nks.[IdUsluge]
, nks.[Naziv]
, nks.Kolicina
, p.Naziv as kupac
, p.Id as kupacId
, p.Adresa
, p.Telefon
, nkz.[BrojDokumenta] AS nalog
, nkz.[BrojDokumentaKroz] AS nalogKroz
, nkz.[RokIsporuke]
, nkz.[IdNastaloOdDokumenta]
, d.Naziv as drzava
FROM [dbo].[NarudzbaKupacaStavke] nks
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz
ON nkz.Id = nks.IdNarudzbe
LEFT JOIN dbo.Partneri p
ON nkz.IdKupac = p.Id
LEFT JOIN dbo.Drzave d
ON p.IdDrzava = d.Id
WHERE idArtikla IN ('FP80PUR-08', 'FP80PUR-09', 'FP80PUR-12')
AND nkz.[VrstaDokumenta] = 'PRO'
AND nkz.StatusArhive = 0
--...
from first query nkz.[IdNastaloOdDokumenta] is important to second
SELECT BrojDokumenta
, BrojDokumentaKroz
FROM .[dbo].[NarudzbeKupacaZaglavlje]
where id = nkz.[IdNastaloOdDokumenta]
For ex. In first query I got nkz.[IdNastaloOdDokumenta] = 20. Number 20 I use in second query in where statement, and value I get from BrojDokumenta, I would like to join to first query.
I was wondering if is possible to make one query out of these two. I think I can not union operator because number of column from these two queries don't match.
The same table, and the same columns are already in the first query. Perhaps you want a self-join, like this:
FROM [dbo].[NarudzbaKupacaStavke] nks
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz
ON nkz.Id = nks.IdNarudzbe
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz2
ON nkz2.Id = nkz.[IdNastaloOdDokumenta]
LEFT JOIN dbo.Partneri p
ON nkz.IdKupac = p.Id
LEFT JOIN dbo.Drzave d
ON p.IdDrzava = d.Id
WHERE idArtikla IN ('FP80PUR-08', 'FP80PUR-09', 'FP80PUR-12')
AND nkz.[VrstaDokumenta] = 'PRO'
AND nkz.StatusArhive = 0

Joining reports

I am trying to join 3 reports into one, taking parts out of each report.
This is my script that works with 2 of the reports:
ALTER VIEW [dbo].[v_JB2] AS
SELECT R.*, I.ENTERED, I.PROBLEM_CODE, I.WORKORDER
FROM DALE.DBO.V_JBTRB R
JOIN REPORTS.DBO.V_TC_ANALYSIS_JEREMY I
ON R.HUBID = I.HUB
AND R.NODEID = I.NODE
AND CAST(R.CREATE_DATE AS DATE) = I.ENTERED_DATE
GO
and I want to add this field A.CREATE_DATE-O.ACTUAL_START_DATE AS ASSIGN_SECS from what should be DALE.DBO.V_MTTA
Your join of DALE.DBO.V_MTTA has no ON condition...
Try this:
SELECT R.*, I.ENTERED, I.PROBLEM_CODE, I.WORKORDER,
A.CREATE_DATE-O.ACTUAL_START_DATE as ASSIGN_SECS
FROM
DALE.DBO.V_JBTRB R JOIN
REPORTS.DBO.V_TC_ANALYSIS_JEREMY I
ON R.HUBID = I.HUB AND R.NODEID = I.NODE AND
CAST(R.CREATE_DATE AS DATE) = I.ENTERED_DATE
JOIN DALE.DBO.V_MTTA A ON A.CREATE_DATE-O.ACTUAL_START_DATE = ??? (what do you want this to be joined on? I don't know how your tables are related, but you need a valid ON statement for this join to work)
so the right answer was and i don't think anyone would have been able to tell based on the code was instead of joining a third query i added to my trb query and got the same data not sure why i didn't think of it sooner.
ALTER VIEW [dbo].[v_JBTRB] AS
SELECT SINGLE_USER, RECORD_ID, DIVISION, CREATE_DATETIMEINNEW, OUTAGESTARTDATE, STATUS_DESC, CAST(HUBID AS VARCHAR(255)) AS HUBID, CAST(NODEID AS VARCHAR(255)) AS NODEID, CATEGORY, STATUS, ASSIGN_SECS
FROM OPENQUERY(REMEDY_BI,
'
SELECT
T.RECORD_ID AS Record_ID
, T.DIVISION AS Division
, T.CREATE_DATE_ET AS Create_Date
, T.TIME_IN_NEW AS TimeInNew
, O.ACTUAL_START_DATE_ET AS OutageStartDate
, T.STATUS_DESC
, T.FACILITY AS HubID
, T.NODE AS NodeID
, T.CATEGORY
, T.STATUS
, T.SINGLE_USER
, T.CREATE_DATE-O.ACTUAL_START_DATE AS ASSIGN_SECS
FROM ARNEUSER.VW_BASE_TROUBLE T
JOIN ARNEUSER.VW_BASE_OUTAGE O
ON O.INSTANCE_ID = T.OUTAGE_INSTANCE_ID
AND O.SUBMITTED_BY = T.SUBMITTER
JOIN ARNEUSER.VW_BASE_TROUBLE_TKT_ASGN_HIS A
ON A.TROUBLE_ID = T.TROUBLE_ID
AND A.CREATE_DATE = ( SELECT MIN(CREATE_DATE)
FROM ARNEUSER.VW_BASE_TROUBLE_TKT_ASGN_HIS
WHERE TROUBLE_ID=T.TROUBLE_ID
AND STATUS_NUM=1
AND CREATE_DATE>=O.ACTUAL_START_DATE
AND SUBMITTER=T.SUBMITTER )
WHERE T.STATUS > 3
AND T.REGION = ''Carolina''
AND T.CREATE_DATE >= DATE_TO_UNIX_TZ(TRUNC(SYSDATE)-14)
AND T.CATEGORY IN (''HFC'',''CRITICAL INFRASTRUCTURE'',''VIDEO DIGITAL'',''VIDEO ANALOG'',''DIGITAL PHONE'',''HEADEND/HUB'',''METRO/REGIONAL NETWORK'',''NATIONAL BACKBONE'',''NETWORK'')
')
I added this part the rest was already there if that helps this is one of the reports i was originally joining. I was trying to join another report but it came from the same data base.
, T.CREATE_DATE-O.ACTUAL_START_DATE AS ASSIGN_SECS
AND A.CREATE_DATE = ( SELECT MIN(CREATE_DATE)
FROM ARNEUSER.VW_BASE_TROUBLE_TKT_ASGN_HIS
WHERE TROUBLE_ID=T.TROUBLE_ID
AND STATUS_NUM=1
AND CREATE_DATE>=O.ACTUAL_START_DATE
AND SUBMITTER=T.SUBMITTER )
this is the other query that was being joined for anyone curious the 8 **** replace some sensitive data
ALTER VIEW [dbo].[v_TC_ANALYSIS_JEREMY] AS
SELECT *, cast(entered_date + ' ' + entered_time as datetime) as ENTERED FROM OPENQUERY(ICOMS_H,'
SELECT
W.WONUM AS WORKORDER,
W.WOTYC AS TYPE,
CVGDT2DATE(W.WOEDT) AS ENTERED_DATE,
W.WOQCD AS QUEUE_CODE,
W.WOETM AS TIME_ENTERED,
W.WOPB1 AS PROBLEM_CODE,
TRIM(H.HOAAEQ) AS HUB,
TRIM(H.HONODE) AS NODE,
H.HOZIP5 AS ZIPCODE,
W.WOPOL AS POOL,
P.EXTXD0 AS AREA,
CVGTM2TIME(W.WOETM) AS ENTERED_TIME
FROM
********.WOMHIPF W
JOIN ******.HOSTPF H ON H.HONUM = W.WOHNUM
JOIN CF83PF P ON P.EXPLLL = W.WOPOL
WHERE
W.WOEDT >= REPLACE(CHAR(CURRENT_DATE - 14 DAYS,ISO),''-'','''')-19000000
AND ((WOTYC =''SR'' AND WOQCD IN (''O'',''M'')) OR (WOTYC =''TC''))
AND WOPOL IN (''1'',''2'',''3'',''4'',''6'',''7'',''E'',''M'',''R'')

Major issues with a query

I have a query
SELECT
ZEML.ICC_CODE AS ICC_CODE
,SUM(CS.TOT_HOURS) AS TOT_HOURS
,SUM(CS.NUM_INCIDENT_ALL) AS NUM_INCIDENTS
,(VALUE(FLOAT(SUM(CS.NUM_INCIDENT_ALL)) * 200000 / SUM(TOT_HOURS)
,0)) AS INC_RATE
FROM TR.CLAIMS_SUMM CS
INNER JOIN TR.LOCATION_MASTER LM
ON LM.LOCATION = CS.LOCATION
AND CS.LOCATION < '900'
LEFT JOIN TR.LOCATION_ASSIGNMENTS DISTRICT
ON DISTRICT.LOCATION = LM.LOCATION
AND DISTRICT.ASSIGNMENT_TYPE = 'District'
LEFT JOIN TR.LOCATION_ASSIGNMENTS TERRITORY
ON TERRITORY.LOCATION = LM.LOCATION
AND TERRITORY.ASSIGNMENT_TYPE = 'Territory'
LEFT JOIN TR.EMPL_CLAIMS ZEML
ON CS.LOCATION = ZEML.LOCATION
AND ZEML.TYPE = 'WC'
AND ZEML.STATUS <> 'V'
AND ZEML.CLAIM_ACTION NOT IN ('D','F','I','H')
WHERE CS.DW_DATE BETWEEN '01/01/2014'
AND '05/31/2014'
AND (MONTH(ZEML.DATE_OF_INCIDENT) = MONTH(CS.DW_DATE)
AND YEAR(ZEML.DATE_OF_INCIDENT) = YEAR(CS.DW_DATE))
GROUP BY ZEML.ICC_CODE
UNION
SELECT
'OTHER' AS ICC_CODE
, 0 AS TOT_HOURS
, 0 AS NUM_INCIDENTS
, 0 AS INC_RATE
FROM SYSIBM.SYSDUMMY1
WHERE 1 = 1
ORDER BY 1
in my union where I made an other I want to select everything else from the tr.empl_claims table and store it in the other from the union because this is what I have many other ICC codes without incidents on them and I am doing calculations on our incident rate and hourse based off of all the data but my query right now is only selecting the ones that currently is having incidents which is throwing off my calculations.
From your use of FROM SYSIBM.SYSDUMMY1 I believe you are using DB2 database. If yes, you can use CTE (common table expression) to achieve the desired result like
WITH cte1 AS
(
SELECT
ZEML.ICC_CODE AS ICC_CODE
,SUM(CS.TOT_HOURS) AS TOT_HOURS
,SUM(CS.NUM_INCIDENT_ALL) AS NUM_INCIDENTS
,(VALUE(FLOAT(SUM(CS.NUM_INCIDENT_ALL)) * 200000 / SUM(TOT_HOURS)
,0)) AS INC_RATE
FROM TR.CLAIMS_SUMM CS
... <rest of the code> ...
)
select * from cte1
UNION ALL
SELECT
ICC_CODE
, 0 AS TOT_HOURS
, 0 AS NUM_INCIDENTS
, 0 AS INC_RATE
FROM TR.EMPL_CLAIMS
WHERE ICC_CODE NOT IN
(
SELECT distinct ICC_CODE
FROM cte1
)
ORDER BY 1
SideNote: You are joining the same table LOCATION_ASSIGNMENTS twice (as below) which is not needed.
LEFT JOIN TR.LOCATION_ASSIGNMENTS DISTRICT
ON DISTRICT.LOCATION = LM.LOCATION
AND DISTRICT.ASSIGNMENT_TYPE = 'District'
LEFT JOIN TR.LOCATION_ASSIGNMENTS TERRITORY
ON TERRITORY.LOCATION = LM.LOCATION
AND TERRITORY.ASSIGNMENT_TYPE = 'Territory'
This can be transformed to below using a IN operator
LEFT JOIN TR.LOCATION_ASSIGNMENTS DISTRICT
ON DISTRICT.LOCATION = LM.LOCATION
AND DISTRICT.ASSIGNMENT_TYPE IN ('District', 'Territory')
See more about Common Table Expression in DB2 Here.
Hope this helps.