Oracle 9i SQL query - join multiple time the same table - sql

I've a strange behavior with a simple SQL query. I work on Oracle 9i.
I just wanna be sure there is no stupid error in my query. If anyone can tell me what I'm doing wrong. Here's the query :
select p.PARTNUM,
p.N_DATAA, dataa1.ID_DATA as ID_DATAA,
p.N_DATAF, dataf1.ID_DATA as ID_DATAF,
p.N_DATAS, datas1.ID_DATA as ID_DATAS
from PIECES p
left join DATA dataa1 on
dataa1.N_DATA = p.N_DATAA AND
dataa1.REV_DATA = ( select max(dataa2.REV_DATA) from DATA dataa2
where dataa2.N_DATA = p.N_DATAA )
left join DATA dataf1 on
dataf1.N_DATA = p.N_DATAF AND
dataf1.REV_DATA = ( select max(dataf2.REV_DATA) from DATA dataf2
where dataf2.N_DATA = p.N_DATAF )
left join DATA datas1 on
datas1.N_DATA = p.N_DATAS AND
datas1.REV_DATA = ( select max(datas2.REV_DATA) from DATA datas2
where datas2.N_DATA = p.N_DATAS )
where p.PARTNUM='MYPARTNUM';
and here is the result :
"PARTNUM" "N_DATAA" "ID_DATAA" "N_DATAF" "ID_DATAF" "N_DATAS" "ID_DATAS"
"MYPARTNUM" "A23240" "300" "F4130" "398" "S2330" ""
My problem appears on the field ID_DATAS (an ID_DATAS should be returned, I'm sure, see below), it acts as if it ignores the last join and retrieve no data. If I delete the SECOND join, it works perfectly. It seems like if Oracle does not support more than 2 left join... (???)
Here an other example when I switch the two last left join (in this case the problem appears on ID_DATAF) :
select p.PARTNUM,
p.N_DATAA, dataa1.ID_DATA as ID_DATAA,
p.N_DATAF, dataf1.ID_DATA as ID_DATAF,
p.N_DATAS, datas1.ID_DATA as ID_DATAS
from PIECES p
left join DATA dataa1 on
dataa1.N_DATA = p.N_DATAA AND
dataa1.REV_DATA = ( select max(dataa2.REV_DATA) from DATA dataa2
where dataa2.N_DATA = p.N_DATAA )
left join DATA datas1 on
datas1.N_DATA = p.N_DATAS AND
datas1.REV_DATA = ( select max(datas2.REV_DATA) from DATA datas2
where datas2.N_DATA = p.N_DATAS )
left join DATA dataf1 on
dataf1.N_DATA = p.N_DATAF AND
dataf1.REV_DATA = ( select max(dataf2.REV_DATA) from DATA dataf2
where dataf2.N_DATA = p.N_DATAF )
where p.PARTNUM='MYPARTNUM';
and here is the result :
"PARTNUM" "N_DATAA" "ID_DATAA" "N_DATAF" "ID_DATAF" "N_DATAS" "ID_DATAS"
"MYPARTNUM" "A23240" "300" "F4130" "" "S2330" "400"
Thank you for your help :)
EDIT : add fully qualified column names in queries (does not solve my problem)

You are probably experiencing a bug with your old version of Oracle. Oracle 9i was the first version to support ANSI join and came with a few bugs with ANSI-join syntax.
Your query won't run in an up-to-date (11.2) Oracle db (SQLFiddle), you will run into:
ORA-01799: a column may not be outer-joined to a subquery
So the bug in your Oracle version is that this query shouldn't return any result. In any case make sure that you're running the latest patch set. Also it may be a good idea to plan an upgrade, this version is deprecated -- extended support ended 3 years ago!
You'll have to rewrite your query, for instance like this:
SQL> WITH filtered_data AS (
2 SELECT n_data, rev_data, id_data
3 FROM DATA d
4 WHERE rev_data = (SELECT MAX(rev_data)
5 FROM DATA d_in
6 WHERE d_in.n_data = d.n_data)
7 )
8 SELECT p.partnum,
9 p.n_dataa,
10 A.id_data AS id_dataa,
11 p.n_dataf,
12 F.id_data AS id_dataf,
13 p.n_datas,
14 S.id_data AS id_datas
15 FROM pieces p
16 LEFT JOIN filtered_data A ON A.n_data = p.n_dataa
17 LEFT JOIN filtered_data S ON S.n_data = p.n_datas
18 LEFT JOIN filtered_data F ON F.n_data = p.n_dataf
19 WHERE p.PARTNUM = 'MYPARTNUM';
PARTNUM N_DATAA ID_DATAA N_DATAF ID_DATAF N_DATAS ID_DATAS
------------- -------- --------- -------- --------- -------- ---------
MYPARTNUM A23240 300 F4130 398 S2330 400

Related

T-SQL subselect statement is returning all rows instead of limiting to 1 based on subselect

I am trying to return just the first row where the BLOCK_STOP_ORDER = 2. What is wrong with my SQL? Why isn't WHERE SCHEDULE.BLOCK_STOP_ORDER = (SELECT MIN(S1.BLOCK_STOP_ORDER....
working? When I run the subselect on its own it returns the value '2' - doesn't that mean it should then limit the query result to only the row(s) where BLOCK_STOP_ORDER = 2?
SELECT ROUTE.ROUTE_ABBR, SCHEDULE.ROUTE_DIRECTION_ID, SCHEDULE.PATTERN_ID, SCHEDULE.BLOCK_STOP_ORDER,
SCHEDULE.SCHEDULED_TIME, GEO_NODE.GEO_NODE_ABBR, TRIP.TRIP_SEQUENCE AS TPST
FROM SCHEDULE
INNER JOIN GEO_NODE ON SCHEDULE.GEO_NODE_ID = GEO_NODE.GEO_NODE_ID
INNER JOIN ROUTE ON SCHEDULE.ROUTE_ID = ROUTE.ROUTE_ID
INNER JOIN TRIP ON SCHEDULE.TRIP_ID = TRIP.TRIP_ID
WHERE (SCHEDULE.CALENDAR_ID = '120221024') AND ROUTE.ROUTE_ABBR = '001'
AND SCHEDULE.ROUTE_DIRECTION_ID = '2' AND SCHEDULE.PATTERN_ID = '270082'
AND TRIP.TRIP_SEQUENCE = '18600'
AND SCHEDULE.BLOCK_STOP_ORDER =
(SELECT MIN(S1.BLOCK_STOP_ORDER)
FROM SCHEDULE S1
WHERE SCHEDULE.CALENDAR_ID = S1.CALENDAR_ID
AND SCHEDULE.ROUTE_ID = S1.ROUTE_ID
AND SCHEDULE.ROUTE_DIRECTION_ID = S1.ROUTE_DIRECTION_ID
AND SCHEDULE.PATTERN_ID = S1.PATTERN_ID
AND SCHEDULE.SCHEDULED_TIME = S1.SCHEDULED_TIME
AND SCHEDULE.GEO_NODE_ID = S1.GEO_NODE_ID
AND SCHEDULE.BLOCK_STOP_ORDER = S1.BLOCK_STOP_ORDER
AND SCHEDULE.TRIP_ID = S1.TRIP_ID
)
GROUP BY ROUTE.ROUTE_ABBR, SCHEDULE.ROUTE_DIRECTION_ID,
SCHEDULE.PATTERN_ID, SCHEDULE.SCHEDULED_TIME,
GEO_NODE.GEO_NODE_ABBR, SCHEDULE.BLOCK_STOP_ORDER, TRIP.TRIP_SEQUENCE
ORDER BY ROUTE.ROUTE_ABBR, SCHEDULE.ROUTE_DIRECTION_ID, TRIP.TRIP_SEQUENCE
Results:
ROUTE_ABBR
ROUTE_DIRECTION_ID
PATTERN_ID
BLOCK_STOP_ORDER
SCHEDULED_TIME
GEO_NODE_ABBR
TPST
001
2
270082
2
18600
1251
18600
001
2
270082
3
18600
1346
18600
001
2
270082
5
18720
1123
18600
001
2
270082
6
18720
11372
18600
001
2
270082
4
18720
1570
18600
001
2
270082
8
18780
11373
18600
This is probably better solved with the row_number() windowing function:
SELECT *
FROM (
SELECT DISTINCT r.ROUTE_ABBR, s.ROUTE_DIRECTION_ID, s.PATTERN_ID, s.BLOCK_STOP_ORDER,
s.SCHEDULED_TIME, g.GEO_NODE_ABBR, t.TRIP_SEQUENCE AS TPST,
row_number() over (order by SCHEDULE.BLOCK_STOP_ORDER) rn
FROM SCHEDULE s
INNER JOIN GEO_NODE g ON s.GEO_NODE_ID = g.GEO_NODE_ID
INNER JOIN ROUTE r ON s.ROUTE_ID = r.ROUTE_ID
INNER JOIN TRIP t ON s.TRIP_ID = t.TRIP_ID
WHERE s.CALENDAR_ID = '120221024' AND r.ROUTE_ABBR = '001'
AND s.ROUTE_DIRECTION_ID = '2' AND s.PATTERN_ID = '270082'
AND t.TRIP_SEQUENCE = '18600'
) t1
WHERE rn=1
ORDER BY t1.ROUTE_ABBR, t1.ROUTE_DIRECTION_ID, t1.TRIP_SEQUENCE
The problem with the original is the name SCHEDULE. For the full version of the query, the subquery is matching the name in the nested select with the instance of the table from the outer select. This correlates the results of the inner table with the outer, so only the item from that row of the outer table is eligible.
When you run the inner query by itself, separate from the outer query, there is only the one instance of the table. In that situation the WHERE conditions are matching the table to itself — they are always true — and you just get the smallest value of all the rows: 2.
This is why you should ALWAYS give ALL the tables in your queries an alias, and ONLY reference them by that alias (as I did in my answer). Do this, and the MIN() version can work... but will still be slower and more code than using row_number().
Finally, the use of DISTINCT / GROUP BY with every SELECT column is usually an indicator you don't fully understand the JOIN relationships used in the query, and in at least one case the join conditions are not sufficiently selective. I'd hesitate to move a query like that to production, even if it seems to be working, though I confess most of us have done it at some point anyway.

How to combine multiple complex queries?

There is a query that displays data by id (R_PERS_ACCOUNT_ID) and date (MAX(RBS.CREATE_DATE))
Select rpao.r_pers_account_id, max(rbs.create_date)
from r_base_trans rbs
join r_pers_acc_operation rpao on rbs.r_base_trans_id = rpao.r_base_trans_id
where rbs.create_date between to_date('01.12.2017', 'dd.mm.yyyy') and to_date('31.12.2020', 'dd.mm.yyyy')
and rbs.M_BASE_TRANS_TYPE_ID NOT IN 26
and ROWNUM < 100
group by rpao.r_pers_account_id;
enter image description here
There is one more request in which you need to insert data from the previous select. In the where clause where pa.r_pers_account_id, you need to insert the id from the previous table. And in to_date('31-01-2018', 'dd-mm-yyyy') there is also date data from the previous table. (In my case, I manually inserted only one data)
select TP.IIN_BIN,
pa.r_pers_account_id,
pa.close_date,
kbk.kbk_code,
org.code_nk,
org.CODE_TPK,
op.m_operation_type_id,
pa.open_date,
sum(op.amount)
from r_pers_account pa
join r_tax_payer tp on pa.r_tax_payer_id = tp.r_tax_payer_id
join r_pers_acc_operation op on op.r_pers_account_id = pa.r_pers_account_id
join m_kbk kbk on kbk.m_kbk_id = pa.m_kbk_id
join m_tax_org org on org.m_tax_org_id = pa.m_tax_org_id
where pa.r_pers_account_id in (16616864)
and is_charge_fine = 0
and trunc(op.actual_date, 'fmdd') <= to_date('31-01-2018', 'dd-mm-yyyy')
and op.m_operation_type_id = 1
group by tp.IIN_BIN, pa.r_pers_account_id, pa.close_date, kbk.kbk_code, op.m_operation_type_id, org.code_nk,
org.code_tpk, pa.open_date;
enter image description here
In this select, you also need to insert data by id and date.
select TP.IIN_BIN,
pa.r_pers_account_id,
pa.close_date,
kbk.kbk_code,
org.code_nk,
org.CODE_TPK,
op.m_operation_type_id,
pa.open_date,
sum(op.amount)
from r_pers_account pa
join r_tax_payer tp on pa.r_tax_payer_id = tp.r_tax_payer_id
join r_pers_acc_operation op on op.r_pers_account_id = pa.r_pers_account_id
join m_kbk kbk on kbk.m_kbk_id = pa.m_kbk_id
join m_tax_org org on org.m_tax_org_id = pa.m_tax_org_id
where pa.r_pers_account_id in (16616864)
and is_charge_fine = 0
and trunc(op.actual_date, 'fmdd') <= to_date('31-01-2018', 'dd-mm-yyyy')
and op.m_operation_type_id = 2
group by tp.IIN_BIN, pa.r_pers_account_id, pa.close_date, kbk.kbk_code, op.m_operation_type_id, org.code_nk,
org.code_tpk, pa.open_date;
enter image description here
It is necessary to make so that these 3 requests were one select.
In addition, after combining these queries, you need to display data by condition if the second select column sum(op.amount) has a negative number, and the third select column sum(op.amount) has 0 or a positive number
I don't quite understand what queries you posted do (2nd and 3rd look just the same to me), but - generally speaking - if you want to "reuse" one query in queries that follow, there's a useful option: CTE (common table expression, i.e. the WITH factoring clause).
Simplified, it would look like this; I hope you'll manage to apply it to your code:
with
first_query as
(select rpao.r_pers_account_id, ...
from ...
where ...
),
second_query as
(select tp.iin_bin, ...
from FIRST_QUERY f1 join r_per_account pa on ...
--------------
-- this is new!
join ...
where ...
),
third_query as
(select tp.iin_bin, ...
from ...
-- use SECOND_QUERY (and/or FIRST_QUERY, if you have to)
where ...
)
-- Finally: extract data you really need
select ...
from third_query t3
where ...

where statement execute before inner join

I'm trying to grab the first instance of each result with a sysAddress of less than 4. However my statement currently grabs the min(actionTime) result first before applying the where sysAddress < 4. I'm trying to have the input for the inner join as the where sysAddress < 4 however i cant seem to figure out how to do it.
Should i be nesting it all differently? I didnt want to create an additional layer of table joins. Is this possible? I'm a bit lost at all the answers ive found.
SELECT
tblHistoryObject.info,
tblHistory.actionTime,
tblHistoryUser.userID,
tblHistoryUser.firstName,
tblHistoryUser.surname,
tblHistory.eventID,
tblHistoryObject.objectID,
tblHistorySystem.sysAddress
FROM tblHistoryObject
JOIN tblHistory
ON (tblHistory.historyObjectID = tblHistoryObject.historyObjectID)
JOIN tblHistorySystem
ON (tblHistory.historySystemID = tblHistorySystem.historySystemID)
JOIN tblHistoryUser
ON (tblHistory.historyUserID = tblHistoryUser.historyUserID)
INNER JOIN (SELECT
MIN(actionTime) AS recent_date,
historyObjectID
FROM tblHistory
GROUP BY historyObjectID) AS t2
ON t2.historyObjectID = tblHistoryObject.historyObjectID
AND tblHistory.actionTime = t2.recent_date
WHERE sysAddress < 4
ORDER BY actionTime ASC
WITH
all_action_times AS
(
SELECT
tblHistoryObject.info,
tblHistory.actionTime,
tblHistoryUser.userID,
tblHistoryUser.firstName,
tblHistoryUser.surname,
tblHistory.eventID,
tblHistoryObject.objectID,
tblHistorySystem.sysAddress,
ROW_NUMBER() OVER (PARTITION BY tblHistoryObject.historyObjectID
ORDER BY tblHistory.actionTime
)
AS historyObjectID_SeqByActionTime
FROM
tblHistoryObject
INNER JOIN
tblHistory
ON tblHistory.historyObjectID = tblHistoryObject.historyObjectID
INNER JOIN
tblHistorySystem
ON tblHistory.historySystemID = tblHistorySystem.historySystemID
INNER JOIN
tblHistoryUser
ON tblHistory.historyUserID = tblHistoryUser.historyUserID
WHERE
tblHistorySystem.sysAddress < 4
)
SELECT
*
FROM
all_action_times
WHERE
historyObjectID_SeqByActionTime = 1
ORDER BY
actionTime ASC
This does exactly what your original query did, without trying to filter by action_time.
Then it appends a new column, using ROW_NUMBER() to generate sequences from 1 for each individual tblHistoryObject.historyObjectID. Then it takes only the rows where this sequence value is 1 (the first row per historyObjectID, when sorted in action_time order).

MSSQL 2012 - Why is AVG & TRY_CONVERT not returning the correct value?

I am using MSSQL 2012 and I am trying to use AVG together with TRY_CONVERT on a table column with the following datatype: nvarchar(255), NOT NULL
First before I try to query using AVG & TRY_CONVERT, this is the data that I want to get the AVG value out of using this query:
And this is the results after using AVG and TRY_CONVERT, 0 rows returned.
I also tried to use a subquery then i got 18 row returned, but with value NULL, i skipped out on AVG just to see if i was getting the correct values.. but it seems not, i also included the p.serialnumber column to show that its the correct rows that was returned, its just the value NULL that somehow appears after TRY_CONVERT.
UPDATE!:
When I execute the query below which target data that has a "." separator (qtv2.qtv_qteid = 58 (instead of 63)) , it works! So the issue is the "," separator. Anyone know solution to this??
declare #ProjectSelection nvarchar(10)
set #ProjectSelection = 'C82007588'
SELECT AVG(TRY_CONVERT(numeric(10,5), avgcap))
FROM
(
select qtv2.qtv_result as avgcap
from ProductionOrder PO
left join CustomerOrder co on co.CustomerOrderId=po.customerorderid
left join ProductionOrderProperty pop on pop.ProductionOrderId=po.productionorderid
left join product p on p.ProductionOrderId=po.productionorderid
left join QualityTestValues qtv on qtv.qtv_productid=p.ProductId
left join QualityTestValues qtv2 on qtv2.qtv_productId=p.ProductId
where pop.Value=#ProjectSelection and pop.name = 'project' and po.ProductTypeId = 1
and qtv2.qtv_qteid = 58 and qtv2.qtv_valid = 1 and qtv.qtv_ProductSegmentId = 144 and qtv.qtv_valid = 1
and qtv.qtv_qteid = 51 and qtv.qtv_result = 'J'
group by co.CustomerName, pop.Value, qtv2.qtv_result, p.SerialNumber
) A
Result:
(No column name)
22.200000
How about your compatibility level? A similar question can be found here:
TRY_CONVERT fails on SQL Server 2012
Although your server version is 2012, a lower compatibility level can cause the try_convert to be unavailable for use in your database. You can check this by running the following code in your specific database and afterwards on for instance the master database.
DECLARE #b VARCHAR(10) = '12312'
SELECT TRY_CONVERT(INT,#b)
You can take your first query and do something like this:
SELECT AVG(TRY_CONVERT(numeric(10,5), avgcap))
FROM
(
-- Insert your first query here.
) A
This should give you the average of the numbers.
EDIT
Here is a workable example that should return 24.000000.
SELECT AVG(TRY_CONVERT(numeric(10,5), A))
FROM
(
SELECT '22.5' AS A
UNION
SELECT '23.5' AS A
UNION
SELECT '26.0' AS A
) B
I found out the solution, using TRY_PARSE instead of TRY_CONVERT.
DECLARE #ProjectSelection nvarchar(10)
SET #ProjectSelection = 'C82007588'
SELECT AVG(avgcap) as avgcap
FROM
(
SELECT qtv2.qtv_result, TRY_PARSE( qtv2.qtv_result AS NUMERIC(10,3)) avgcap
FROM ProductionOrder PO
LEFT JOIN CustomerOrder co on co.CustomerOrderId=po.customerorderid
LEFT JOIN ProductionOrderProperty pop on pop.ProductionOrderId=po.productionorderid
LEFT JOIN product p on p.ProductionOrderId=po.productionorderid
LEFT JOIN QualityTestValues qtv on qtv.qtv_productid=p.ProductId
LEFT JOIN QualityTestValues qtv2 on qtv2.qtv_productId=p.ProductId
WHERE pop.Value=#ProjectSelection
AND pop.name = 'project'
AND po.ProductTypeId = 1
AND qtv2.qtv_qteid = 63
AND qtv2.qtv_valid = 1
AND qtv.qtv_ProductSegmentId = 144
AND qtv.qtv_valid = 1
AND qtv.qtv_qteid = 51
AND qtv.qtv_result = 'J'
GROUP BY co.CustomerName, pop.Value, qtv2.qtv_result, p.SerialNumber
) A`
Results:
avgcap
21264.850000

In Putty, how do you locate where an error is based on the char position?

I received the following error, which is based off of my code (attached below) in Netezza .
^ found "WHERE" (at char 543) expecting an identifier found a keyword
But how I find where in the code this is located at? Do I need to divide 543 by the amount of characters per line ?
the code is shown below :
DELETE FROM TDM_FEE_DISCOUNT_FACT;;
----------------------------------------------------------------------------
-- INSERT INTO TDM TABLE ---
----------------------------------------------------------------------------
INSERT INTO FEE_DISCOUNT_FACT
(
FEE_DISCNT_F_DK ,
HLTH_PLN_GRP_DK,
HLTH_PLN_SPSR_DK,
PLN_MBR_DK,
COV_PRD_STRT_DT,
COV_PRD_END_DT,
BIL_DUE_DT,
FEE_AMT,
DISCNT_AMT,
BIL_ID,
SS_CD,
TNT_CD,
INSRT_DT,
UPDT_DT,
CREAT_RUN_CYC_EXEC_SK,
LST_UPDT_RUN_CYC_EXEC_SK,
REC_PRCS_TYP_CD,
ROW_EFF_STRT_DT,
ROW_EFF_END_DT,
CUR_ROW_IND
)
WITH LAST_RUN_DATE(DT) AS
(
SELECT NVL(LAST_RUN, TO_DATE('01/01/1900', 'MM/DD/YYYY'))
FROM (
SELECT MAX(NVL(TCT.MANIFEST_COMPLETED_TS, TO_DATE('01/01/1900', 'MM/DD/YYYY'))) LAST_RUN
FROM :DB_XREF..TGT_CONTROL_TBL TCT
WHERE MANIFEST = 'F_FEE_DISCOUNT'
) T
)
,
--Need to modify the following section , on line 66 of FEE_DISCOUNT_FACT
DRIVER AS
(
select
PM.HLTH_PLN_GRP_NBR||'|'||SBFD.SS_CD AS HLTH_PLN_GRP_BK
,PM.HLTH_PLN_GRP_NBR AS SPSR_ID
,PM.PLN_MBR_SK
--COV_PRD_STRT_DT
--COV_PRD_END_DT
,SBFD.BIL_DUE_DT AS BIL_DUE_DT
,NVL(SBFD.FEE_AMT,0) AS FEE_AMT
,NVL(SBFD.DISCNT_AMT,0) AS DISCNT_AMT
,SBFD.BIL_ID
,SBFD.SS_CD
,SBFD.FEE_DISCNT_CREAT_DT_TM
from MBRBOR_TGT_D4..SUBSCRIBER_BILLING_FEE_DISCOUNT SBFD
LEFT OUTER JOIN MBRBOR_TGT_D4..PLAN_MEMBER PM
ON PM.SBSCR_SK = SBFD.SBSCR_SK
AND PM.CUR_ROW_IND = 'Y'
AND PM.REC_PRCS_TYP_CD <> 'D'
AND PM.SBSCR_DPND_RLNSP_TYP_CD_SK IN (SELECT CD_MAP_SK FROM REFBOR_TGT_D4..CODEMAP
WHERE CONFOR_CD = 'SUB')
)
/* IN the code, only need to join to the dimensions .. FACT DK's are the sequence */
SELECT NEXT VALUE FOR FEE_DISCOUNT_F_SEQ AS FEE_DISCNT_F_DK,
NVL(HPGD.HLTH_PLN_GRP_DK,-9) AS HLTH_PLN_GRP_DK,
NVL(HPGD.HLTH_PLN_SPSR_DK,-9) AS HLTH_PLN_SPSR_DK,
PM.PLN_MBR_SK AS PLN_MBR_DK,
NVL(??.COV_PRD_STRT_DT,-9) AS COV_PRD_STRT_DT,
NVL(??.COV_PRD_END_DT,-9) AS COV_PRD_END_DT,
NVL(BIL_DUE_DT,0) AS BIL_DUE_DT,
NVL(FEE_AMT, 0) AS FEE_AMT,
NVL(DISCNT_AMT, 0) AS DISCNT_AMT,
NVL(BIL_ID,-9) AS BIL_ID,
DR.SS_CD AS SS_CD,
'BSC' AS TNT_CD,
NOW() AS INSRT_DT,
NOW() AS UPDT_DT,
NVL(SBFD.CREAT_RUN_CYC_EXEC_SK,-9) AS CREAT_RUN_CYC_EXEC_SK,
NVL(SBFD.LST_UPDT_RUN_CYC_EXEC_SK,-9) AS LST_UPDT_RUN_CYC_EXEC_SK,
NVL(SBFD.REC_PRCS_TYP_CD,0) AS REC_PRCS_TYP_CD,
NVL(SBFD.ROW_EFF_STRT_DT,0) AS ROW_EFF_STRT_DT,
NVL(SBFD.ROW_EFF_END_DT,0) AS ROW_EFF_END_DT,
NVL(SBFD.CUR_ROW_IND,0) AS CUR_ROW_IND --FEE_DISCNT_CREAT_DT_TM
FROM DRIVER DR
LEFT OUTER JOIN :DB_TGT..HEALTH_PLAN_GROUP_DIMENSION HPGD ON DR.HLTH_PLN_GRP_BK = HPGD.HLTH_PLN_GRP_BK
AND FEE_DISCNT_CREAT_DT_TM BETWEEN HPGD.ROW_EFF_STRT_DT AND HPGD.ROW_EFF_END_DT
LEFT OUTER JOIN :DB_TGT..HEALTH_PLAN_SPONSOR_DIMENSION HPSD ON DR.SPSR_ID = HPSD.SPSR_ID
AND FEE_DISCNT_CREAT_DT_TM BETWEEN HPSD.ROW_EFF_STRT_DT AND HPSD.ROW_EFF_END_DT --DONE
--some of the joins will have different conditions . like for SS_CD it will be different
LEFT OUTER JOIN :DB_TGT..PLAN_MEMBER_DIMENSION PMD ON DR.PLN_MBR_SK = PMD.PLN_MBR_SK
AND FEE_DISCNT_CREAT_DT_TM BETWEEN PMD.ROW_EFF_STRT_DT AND PMD.ROW_EFF_END_DT
--LEFT OUTER JOIN
LEFT OUTER JOIN
(SELECT SBSCR.PLN_MBR_DK AS SBSCR_DK, PM.*
FROM :DB_TGT..PLAN_MEMBER_DIMENSION PM
LEFT OUTER JOIN
(SELECT PM.PLN_MBR_DK, PM.PLN_MBR_SK, PM.SBSCR_SK, PM.CUR_ROW_IND, PM.ROW_EFF_STRT_DT, PM.ROW_EFF_END_DT FROM
:DB_TGT..PLAN_MEMBER_DIMENSION PM WHERE PLN_MBR_SK = SBSCR_SK) SBSCR
ON PM.SBSCR_SK = SBSCR.SBSCR_SK AND (PM.ROW_EFF_END_DT -1) BETWEEN
SBSCR.ROW_EFF_STRT_DT AND SBSCR.ROW_EFF_END_DT) PMD
ON DR.PLN_MBR_SK = PMD.PLN_MBR_SK
AND DR.CAPITN_ERN_FROM_DT BETWEEN PMD.ROW_EFF_STRT_DT AND PMD.ROW_EFF_END_DT
Thanks
Since vi is the editor used for the queries (if you're in nzsql) - you can navigate 543 characters from the start by typing in 0543l in the editor - this will take you the start, then 543 steps to the "right". This should bring you to the area of the sql where the problem is.