CURSOR is not displaying data/result set - sql

I've tried everything I could and i still can't get my cursor to display data. here is my code:
DECLARE
CURSOR SL_Cur IS
select *
from (
select
cdav.bank_id,
ent.bank_desc Bank_Description,
cdav.sol_id,
sol.sol_desc SOL_Description,
cdav.gl_sub_head_code GLSH_Code,
decode(cdav.gl_sub_head_code,
'10301',1,
'10403',2, '60403',2, '10501',2, '60501',2, '10502',2,
'10503',2, '10504',2, '10505',2, '10507',2, '10509',2,
'60509',2, '10511',2, '10518',2, '60518',2, '10523',2,
'60523',2, '10551',2, '10552',2, '10553',2, '10554',2,
'10555',2, '10557',2, '10559',2, '10561',2, '10568',2,
'10573',2,
'12336',3, '62336',3, '10401',3, '60402',3, 4 ) GLSH_SET ,
gsh.gl_sub_head_desc GLSH_Name,
case
when (cast(substr(cdav.gl_sub_head_code,0,1) as int) >= 1
and cast(substr(cdav.gl_sub_head_code,0,1) as int) <= 5)
then 'R'
when cast(substr(cdav.gl_sub_head_code,0,1) as int) = 0
or (cast(substr(cdav.gl_sub_head_code,0,1) as int) >= 6
and cast(substr(cdav.gl_sub_head_code,0,1) as int) <= 9)
then 'F'
end book_type,
gam.foracid account_number,
gam.acct_name,
cdav.tran_crncy_code Tran_Currency,
cdav.value_date,
cdav.tran_date Transaction_Date,
cdav.gl_date,
cdav.tran_particular,
rank() over(partition by gam.foracid order by eab.eod_date desc) eod_date_rank,
eab.eod_date,
case when
(select tran_date_bal from tbaadm.eab
where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab
where cdav.acid = eab.acid and eab.eod_date < '28-MAY-2013')
and cdav.acid = eab.acid
and cdav.bank_id = eab.bank_id) is not null
then (select tran_date_bal from tbaadm.eab
where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab
where cdav.acid = eab.acid and eab.eod_date < '28-MAY-2013')
and cdav.acid = eab.acid
and cdav.bank_id = eab.bank_id)
else 0
end beg_tran_date_bal,
(select tran_date_bal from tbaadm.eab eab
where eod_date = (select max(eab.eod_date) from tbaadm.eab eab
where cdav.acid = eab.acid and eab.eod_date <= '28-MAY-2013')
and cdav.acid = eab.acid
and cdav.bank_id = eab.bank_id) end_tran_date_bal,
ott.ref_num OAP_Ref_No,
trim(cdav.tran_id) Transaction_ID,
--cdav.dth_init_sol_id Initiating_SOL_ID,
'PCC_Code',
cdav.tran_rmks Tran_Remarks,
case
when (cdav.part_tran_type = 'D')
then (cdav.tran_amt)
end dr_amount,
case
when (cdav.part_tran_type = 'C')
then (cdav.tran_amt)
end cr_amount
from tbaadm.ctd_dtd_acli_view cdav
left outer join tbaadm.gam
on cdav.bank_id = gam.bank_id and cdav.acid = gam.acid
left outer join tbaadm.gsh
on gam.bank_id = gsh.bank_id and gam.sol_id = gsh.sol_id
and gam.gl_sub_head_code = gsh.gl_sub_head_code
and gam.acct_crncy_code = gsh.crncy_code
left outer join tbaadm.sol
on cdav.bank_id = sol.bank_id and cdav.sol_id = sol.sol_id
left outer join tbaadm.eab
on gam.bank_id = eab.bank_id and gam.acid = eab.acid
left outer join tbaadm.cnc
on gam.bank_id = cnc.bank_id and gam.acct_crncy_code = cnc.crncy_code
left outer join crmuser.end ent
on cdav.bank_id = ent.bank_id
left outer join tbaadm.gct
on cdav.bank_id = gct.bank_id
left outer join tbaadm.ott
on cdav.tran_id = ott.tran_id and cdav.tran_date = ott.tran_date
and cdav.part_tran_srl_num = ott.part_tran_srl_num
and cdav.bank_id = ott.bank_id and cdav.acid = ott.acid
where
gam.acct_ownership = 'O'
and cdav.bank_id = 'CBC01'
and cdav.gl_date = '28-MAY-2013'
and (gam.gl_sub_head_code in ('10301','10403','60403',
'10501','60501','10502','10503','10504','10505',
'10507','10509','60509','10511','10518','60518',
'10523','60523','10551','10552','10553','10554',
'10555','10557','10559','10561','10568','10573',
'12336','62336','10401','60402')
or gam.acct_classification_flg in ('I','E')
)
and trim(cdav.del_flg) in ('N', null)
and trim(gam.del_flg) in ('N', null)
and trim(gsh.del_flg) in ('N', null)
and trim(sol.del_flg) in ('N', null)
and trim(cnc.del_flg) in ('N', null)
and trim(gct.del_flg) in ('N', null)
)
where
eod_date_rank = 1
and GLSH_SET = 1
order by
bank_id,
sol_id,
tran_currency,
glsh_code,
book_type desc,
account_number,
transaction_date;
in_sl_rec SL_Cur%ROWTYPE;
BEGIN
open SL_Cur;
LOOP
FETCH SL_Cur INTO in_sl_rec;
exit when SL_Cur%notfound;
END LOOP;
close SL_Cur;
END;
At first, I just need to make the CURSOR display the result set that I want to see. Next, I want to make the cursor into a sort of FOR LOOP, because there would be input parameters involved when executing the code in iReport, mainly a date range.

Cursors don't just "display" when you execute them - you have to write a little code to do that. Try adding the following lines in your loop after the exit when SL_Cur%notfound; and just before the END LOOP;
DBMS_OUTPUT.PUT_LINE('BANK_ID=' || in_sl_rec.BANK_ID ||
'Bank_Description=' || in_sl_rec.Bank_Description ||
'sol_id=' || in_sl_rec.sol_id ||
'SOL_Description=' || in_sl_rec.SOL_Description);
You can add more fields as you need to.
Share and enjoy.

Related

How can I remove the the union from this code?

I am attempting to remove the union in this sub-select due to performance issues. All attempts are unsuccessful because of how the AllocatedCRGReservedQty is calculated. How can I remove the union and combine the 2 select statements?
I was able to remove the union on the other portion of the query because it was based on a single where clause difference.
SELECT SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,InvQty
,SrdQty
,SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,AllocatedCRGReservedQty
FROM
( SELECT DISTINCT
SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,AsOfInventory InvQty
,SUM(SrdQty)
OVER (PARTITION BY SrdKey) SrdQty
,SUM(SrdSampleApprovedQty)
OVER (PARTITION BY SrdKey) SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,(SELECT SUM(SltLotAllocTranQty)
FROM SalesOrderLot
INNER JOIN
SalesOrderLotTran ON SltSolKey = SolKey
WHERE SolCompanyID = SodCompanyID AND
SolBranchID = SodBranchID AND
SolSalesOrder = SodSalesOrder AND
SolSalesOrderDtlKey = SodSalesOrderDtlKey AND
SolWipFgLotID = SrdLotID) AllocatedCRGReservedQty
FROM SampleRequestHdr
INNER JOIN SampleRequestDtl ON SrdSrhKey = SrhKey
INNER JOIN Warehouse ON WhsWhseID = SrhWhseID
INNER JOIN Lot ON LotCompanyID = SrhCompanyID AND
LotItemID = SrdItemID AND
LotID = SrdLotID
INNER JOIN #AsOfInventory ON IntLotKey = LotKey
-- Only include inventory transactions for completed production orders
-- Changed to temptable due to deadlocks on prodorder/prodorderreservation
INNER JOIN SalesOrderDtl ON SodCompanyID = SrhCompanyID AND
SodBranchID = SrdBranchID AND
SodSalesOrder = SrdSalesOrder AND
SodSalesOrderDtlKey = SrdSalesOrderDtlKey
INNER JOIN SalesOrderHdr ON SohCompanyID = SodCompanyID AND
SohBranchID = SodBranchID AND
SohSalesOrder = SodSalesOrder
WHERE SrhCompanyID = #CompanyID AND
((#PlantID <> '*' AND WhsPlantID = #PlantID) OR (#PlantID = '*')) AND
SrhApprovalExpireDate >= #As_Of_Date AND
ISNULL(SrdSampleRejected,'N') = 'N' AND
SohType IN ('Contract','Normal') AND
SrdQty <> 0
) WORK
WHERE ISNULL(AllocatedCRGReservedQty,0) < SrdQty -- if the lot is already allocated for at least the offered qty then don't include it so we don't over-state the gallons offered
UNION ALL -- then get the sample request qty offered for non-contract sales orders or no sales order assigned
SELECT SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,InvQty
,SrdQty
,SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,AllocatedCRGReservedQty
FROM
( SELECT DISTINCT
SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,AsOfInventory InvQty
,SUM(SrdQty)
OVER (PARTITION BY SrdKey) SrdQty
,SUM(SrdSampleApprovedQty)
OVER (PARTITION BY SrdKey) SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,(SELECT SUM(WptTranQty)
FROM WineProgram
CROSS APPLY
FN_CDV_WineProgramLotTranPlusPending(SrdLotID, WpgKey)
INNER JOIN
IntendedUse ON InuKey = WpgInuKey AND
InuIntendedUse = 'CRG'
LEFT JOIN
--ProdOrdDtl (NOLOCK) ON PodKey = wptPodKey
va_CDV_ProdOrder ON PodKey = wptPodKey
WHERE --WptLotID = SrdLotID AND
ISNULL(WptTranType,'') <> 'Pending' AND
((wptPodKey IS NOT NULL AND
(#CompletePOsOnly = 'N' OR -- include all POs
(#CompletePOsOnly = 'Y' AND PohCompleteDate IS NOT NULL))) -- Only include transactions if PO is Complete or if it's a non-PO transaction
OR
wptPodKey IS NULL
)
) AllocatedCRGReservedQty
FROM SampleRequestHdr
INNER JOIN SampleRequestDtl ON SrdSrhKey = SrhKey
INNER JOIN Warehouse ON WhsWhseID = SrhWhseID
INNER JOIN Lot ON LotCompanyID = SrhCompanyID AND
LotItemID = SrdItemID AND
LotID = SrdLotID
INNER JOIN
#AsOfInventory ON IntLotKey = LotKey
LEFT JOIN SalesOrderDtl ON SodCompanyID = SrhCompanyID AND
SodBranchID = SrdBranchID AND
SodSalesOrder = SrdSalesOrder AND
SodSalesOrderDtlKey = SrdSalesOrderDtlKey
LEFT JOIN SalesOrderHdr ON SohCompanyID = SodCompanyID AND
SohBranchID = SodBranchID AND
SohSalesOrder = SodSalesOrder
WHERE SrhCompanyID = #CompanyID AND
((#PlantID <> '*' AND WhsPlantID = #PlantID) OR (#PlantID = '*')) AND
SrhApprovalExpireDate >= #As_Of_Date AND
ISNULL(SrdSampleRejected,'N') = 'N' AND
ISNULL(SrdUseCRGReservation,'N') = 'N' AND -- if the SrdUseCRGReservation flag = "N" then return the sample qty
ISNULL(SohType,'') NOT IN ('Contract','Normal') AND -- offered to reduce the ATP (pseudo reservation until the sample expires or is approved/rejected)
SrdQty <> 0
) WORK

How can I sum the total based on a column in Firebird 2.5

How can get totalizations out of a group, especifically in Firebird 2.5.
I have the following query, it's big but it's simple, it has only some inner joins that are correlated.
SELECT "patrimônio", porc_residual AS "percentual residual", vida_util AS "vida útil",
vida_util_meses "vida útil meses", valor_base AS "valor aquisição/valor reavaliação",
"data de incorporação", VALOR_DEPRECIADO AS "valor depreciado" ,
grupo_audesp AS "grupo contábil", sum(dt_depreciado) over(ORDER BY grupo_audesp ) td
FROM
(
SELECT m.nome AS "patrimônio", t.PORC_RESIDUAL, t.VIDA_UTIL, t.VIDA_UTIL_MESES,
lpad(EXTRACT(DAY FROM t.dt_cadastro),2, '0') || '/' || lpad(EXTRACT(month FROM t.dt_cadastro),2, '0') || '/' || EXTRACT(year FROM t.dt_cadastro) "data de incorporação"
,t.placa, GA.ID_AUDESP, GA.NOME AS GRUPO_AUDESP,
T.VALOR as valor,
CASE WHEN (SELECT min(D.VALOR_NOVO)
FROM patrimonio_depreciacao D WHERE D.id_tombamento = T.id_tombamento
AND T.ID_ORGAO = '030000' AND T.SITUACAO IN('A','B')
AND D.REFERENCIA2 >=202201 AND D.REFERENCIA2 <=202201 ) IS NULL THEN(
case when (select max(d.valor_anterior)
from patrimonio_depreciacao D where D.id_tombamento = T.id_tombamento and
T.SITUACAO IN('A','B')
AND d.referencia2 >202201 ) IS NULL THEN T.VALOR_ATUAL ELSE
(select max(d.valor_anterior)
from patrimonio_depreciacao D where D.id_tombamento = T.id_tombamento and
T.SITUACAO IN('A','B')
AND d.referencia2 >202201 )
END)
ELSE
(SELECT min(D.VALOR_NOVO)
FROM patrimonio_depreciacao D WHERE D.id_tombamento = T.id_tombamento
AND T.ID_ORGAO = '030000' AND T.SITUACAO IN('A','B')
AND D.REFERENCIA2 >=202201 AND D.REFERENCIA2 <=202201 ) END as valor_atual,
(SELECT sum(D.VALOR_DEPRECIADO)
FROM patrimonio_depreciacao D WHERE D.id_tombamento = T.id_tombamento
AND T.ID_ORGAO = '030000'
AND D.REFERENCIA2 >=202201 AND D.REFERENCIA2 <=202201 ) as valor_depreciado, T.DT_AQUISICAO,
PS.NOME,
case when (select first 1 pri.vl_reavaliacao from patrimonio_reavaliacao_item pri
inner join patrimonio_reavaliacao pr on pr.id_reavaliacao = pri.id_reavaliacao_item
where pri.id_tombamento = t.id_tombamento
and pr.data<='2022-01-31'
and pr.encerrado ='S'
order by pr.data desc) is null then t.vl_base_depreciacao
else
(select first 1 pri.vl_reavaliacao from patrimonio_reavaliacao_item pri
inner join patrimonio_reavaliacao pr on pr.id_reavaliacao = pri.id_reavaliacao_item
where pri.id_tombamento = t.id_tombamento
and pr.data <= '2022-01-31'
and pr.encerrado ='S'
order by pr.data desc) end as valor_base
FROM PATRIMONIO_TOMBAMENTO T
LEFT JOIN PATRIMONIO_GRUPO_AUDESP GA ON GA.ID_GRUPO_AUDESP = T.ID_GRUPO_AUDESP
LEFT JOIN ESTOQUE_MATERIAL M ON M.ID_MATERIAL = T.ID_MATERIAL
LEFT JOIN PATRIMONIO_SETOR PS ON (T.ID_SETOR = PS.ID_SETOR)
WHERE T.ID_ORGAO = '030000'
AND (T.SITUACAO IN('A') or ( T.SITUACAO = 'B' AND T.DT_BAIXA >'2022-01-31'))
AND (T.DT_REATIVADO IS NULL OR T.DT_REATIVADO<= '2022-01-31' or (T.DT_BAIXA >'2022-01-31'))
AND T.dt_cadastro <= '2022-01-31'
AND PS.TIPO_SETOR = 'S'
ORDER BY GA.ID_AUDESP, t.DT_CADASTRO) t
I think most import is the result
I want to sum "valor depreciado" and "valor aquisição/valor reavaliação" based on those groups.
to make it simple it could be an additional column that repeats the sum in all rows for the entire group.

Connecting Tableau to Teradata with Custom SQL Query

EDIT: Even if I try removing that 'AS' everywhere, I continue getting errors about things that seem like normal SQL commands. If I remove the 'AS', the next error I get is:
[IBM][CLI Driver][DB2] SQL0199N The use of the reserved word "CAST"
following "" is not valid. Expected tokens may include: ", )".
SQLSTATE=42601
Question:
I am trying to connect to Teradata with a Custom SQL Query, and while it is running perfectly in Teradata SQL Assistant, I keep getting errors when trying to run it in Tableau.
One of the errors looks like this, but even if I try to change something, a similar error will come up:
[IBM][CLI Driver][DB2] SQL0199N The use of the reserved word "AS"
following "" is not valid. Expected tokens may include: "JOIN CROSS
INNER LEFT RIGHT FULL (". SQLSTATE=42601
Here is the query (sorry, it's long):
WITH RQSTS AS(
select CAST( M.RQST_ID AS VARCHAR(20)) AS MASTER_RQST_ID,
M.RQST_TYP_CD
FROM PRTHD.HOST_RQST AS M --MASTER
WHERE M.MSTR_RQST_ID IS NULL
AND M.crt_user_id='SXB8NBS'
AND M.OCYC_SOQ_RSN_CD = 170
AND date(M.CRT_TS) >= CURRENT_DATE - 7 DAY
),
RQST_TYPE AS(
select R.MASTER_RQST_ID AS RQST_ID,
NRT.TYP_DESC AS HOST_TYPE
FROM RQSTS AS R
LEFT JOIN PRTHD.N_HOST_RQST_TYP AS NRT
ON R.RQST_TYP_CD = NRT.RQST_TYP_CD
GROUP BY R.MASTER_RQST_ID, NRT.TYP_DESC
),
PO_DETAILS AS(
select A.*
FROM (
SELECT distinct PRTHD.BYO_PO.DSVC_TYP_CD,
RT.RQST_ID,
RT.HOST_TYPE,
PRTHD.BYO_PO.PO_CMT_TXT,
PRTHD.BYO_PO.BYO_NBR,
CASE WHEN PRTHD.BYO_PO.DSVC_TYP_CD = 2 THEN RIGHT('00'||PRTHD.BYO_PO.BYO_NBR,2)
ELSE RIGHT('00'|| PRTHD.BYO_PO_LOC_SKU.LOC_NBR,2) END
|| PRTHD.BYO_PO.acct_po_typ_cd || RIGHT('00000'||PRTHD.BYO_PO.PO_CTRL_NBR,5) AS PO,
PRTHD.BYO_PO.ORD_REF_NBR,
PRTHD.BYO_PO_LOC_SKU.MKT_DC_NBR,
PRTHD.BYO_PO_LOC_SKU.LOC_NBR,
PRTHD.BYO_PO_LOC_SKU.SKU_NBR,
CAST(PRTHD.BYO_PO_LOC_SKU.ORD_QTY AS INTEGER) AS ORD_QTY,
PRTHD.BYO_PO.CRT_DT,
PRTHD.BYO_PO.PO_CTRL_NBR,
M.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BYO_PO_LOC_SKU.PO_LINE_NBR = PRTHD.BYO_PO_SKU.PO_LINE_NBR)
LEFT JOIN PRTHD.MVNDR AS M
ON M.MVNDR_NBR = PRTHD.BYO_PO.MVNDR_NBR
INNER JOIN RQST_TYPE AS RT
ON TRIM(SUBSTR(BP.PO_CMT_TXT,6,11)) = RT.RQST_ID
) AS A)
Select P.RQST_ID,
P.DSVC_TYP_CD,
CASE WHEN P.DSVC_TYP_CD =1 THEN 'DTS'
WHEN P.DSVC_TYP_CD =2 THEN 'RDC AGG'
WHEN P.DSVC_TYP_CD =3 THEN 'RDCX'
END AS DSVC_TYP_DESC ,
CASE WHEN P.DSVC_TYP_CD =2 AND S.STR_NBR IS NOT NULL THEN 'Y'
ELSE 'N' END AS RDC_AGG_PEG_FLG,
CASE WHEN P.DSVC_TYP_CD =2 AND S.STR_NBR IS NOT NULL THEN P.ORD_REF_NBR
ELSE P.PO END AS PO_NBR,
P.BYO_NBR,
P.PO_CTRL_NBR,
P.CRT_DT,
P.HOST_TYPE,
P.MVNDR_NBR,
P.MVNDR_NM,
CASE WHEN P.DSVC_TYP_CD =2 AND S.STR_NBR IS NOT NULL THEN AR.MKT_DC_NBR
ELSE P.MKT_DC_NBR END AS MKT_DC_NBR,
P.LOC_NBR,
COALESCE(SKU.MER_DEPT_NBR || '-' || D.SHRT_DEPT_NM,'') AS DEPT,
COALESCE(SKU.MER_CLASS_NBR || '-' || MC.SHRT_CLASS_DESC,'') AS CLASS,
COALESCE(SKU.MER_SUB_CLASS_NBR || '-' || MSC.SHRT_SUBCLASS_DESC,'') AS SUB_CLASS,
P.SKU_NBR,
P.ORD_QTY,
COALESCE(SD.AVG_DC_LCOST_AMT*COALESCE(P.ORD_QTY,0),
MSM.CURR_COST_AMT*COALESCE(P.ORD_QTY,0)) AS TOTAL_COST,
SD.AVG_DC_LCOST_AMT AS SD_COST,
MSM.CURR_COST_AMT AS STR_CURR_COST,
MSD.CURR_COST_AMT AS DC_CURR_COST,
P.PO_CMT_TXT,
P.RSN_CD
FROM PO_DETAILS AS P
--SKU HIERARCHY INFO
LEFT JOIN PRTHD.SKU AS SKU
ON SKU.SKU_NBR = P.SKU_NBR
LEFT JOIN PRTHD.DEPT AS D
ON SKU.MER_DEPT_NBR = D.DEPT_NBR
LEFT JOIN PRTHD.MER_CLASS AS MC
ON MC.MER_DEPT_NBR= SKU.MER_DEPT_NBR
AND MC.MER_CLASS_NBR = SKU.MER_CLASS_NBR
LEFT JOIN PRTHD.MER_SUB_CLASS AS MSC
ON MSC.MER_DEPT_NBR= SKU.MER_DEPT_NBR
AND MSC.MER_CLASS_NBR = SKU.MER_CLASS_NBR
AND MSC.MER_SUB_CLASS_NBR = SKU.MER_SUB_CLASS_NBR
LEFT JOIN PRTHD.MVNDR_SKU_DC AS MSD
ON MSD.MVNDR_NBR = P.MVNDR_NBR
AND MSD.SKU_NBR = P.SKU_NBR
AND MSD.DC_NBR = P.LOC_NBR
LEFT JOIN PRTHD.STR AS S
ON S.STR_NBR = P.LOC_NBR
LEFT JOIN PRTHD.SKU_DC AS SD
ON SD.DC_NBR=P.MKT_DC_NBR
AND SD.SKU_NBR=P.SKU_NBR
LEFT JOIN PRTHD.MVNDR_SKU_MKT AS MSM
ON MSM.MVNDR_NBR = P.MVNDR_NBR
AND MSM.SKU_NBR = P.SKU_NBR
AND MSM.MKT_NBR = S.MKT_NBR
LEFT JOIN PRTHD.AGG_RQST AS AR
ON AR.PO_NBR = P.ORD_REF_NBR
AND P.MVNDR_NBR = AR.MVNDR_NBR
AND SKU.MER_DEPT_NBR = AR.MER_DEPT_NBR
AND AR.ORD_DT >= P.CRT_DT
Can anyone help me figure out what might be going wrong here? Thank you!
Delete all the AS:
WITH RQSTS (
select CT(M.RQST_ID VARCHAR(20)) MTER_RQST_ID,
M.RQST_TYP_CD
FROM PRTHD.HOST_RQST M --MTER
WHERE M.MSTR_RQST_ID IS NULL
AND M.crt_user_id = 'SXB8NBS'
AND M.OCYC_SOQ_RSN_CD = 170
AND date(M.CRT_TS) >= CURRENT_DATE - 7 DAY
),
RQST_TYPE (
select R.MTER_RQST_ID RQST_ID,
NRT.TYP_DESC HOST_TYPE
FROM RQSTS R
LEFT JOIN PRTHD.N_HOST_RQST_TYP NRT ON R.RQST_TYP_CD = NRT.RQST_TYP_CD
GROUP BY R.MTER_RQST_ID,
NRT.TYP_DESC
),
PO_DETAILS (
select A.*
FROM (
SELECT distinct PRTHD.BYO_PO.DSVC_TYP_CD,
RT.RQST_ID,
RT.HOST_TYPE,
PRTHD.BYO_PO.PO_CMT_TXT,
PRTHD.BYO_PO.BYO_NBR,
CE
WHEN PRTHD.BYO_PO.DSVC_TYP_CD = 2 THEN RIGHT('00' || PRTHD.BYO_PO.BYO_NBR, 2)
ELSE RIGHT('00' || PRTHD.BYO_PO_LOC_SKU.LOC_NBR, 2)
END || PRTHD.BYO_PO.acct_po_typ_cd || RIGHT('00000' || PRTHD.BYO_PO.PO_CTRL_NBR, 5) PO,
PRTHD.BYO_PO.ORD_REF_NBR,
PRTHD.BYO_PO_LOC_SKU.MKT_DC_NBR,
PRTHD.BYO_PO_LOC_SKU.LOC_NBR,
PRTHD.BYO_PO_LOC_SKU.SKU_NBR,
CT(PRTHD.BYO_PO_LOC_SKU.ORD_QTY INTEGER) ORD_QTY,
PRTHD.BYO_PO.CRT_DT,
PRTHD.BYO_PO.PO_CTRL_NBR,
M.MVNDR_NM,
PRTHD.BYO_PO.MVNDR_NBR,
PRTHD.BYO_PO.OCYC_SOQ_RSN_CD RSN_CD
FROM (
(
PRTHD.BYO_PO BP
INNER JOIN PRTHD.BYO_PO_LOC_SKU ON (
PRTHD.BYO_PO.ORD_SEQ_NBR = PRTHD.BYO_PO_LOC_SKU.ORD_SEQ_NBR
)
AND (
PRTHD.BYO_PO.PO_CTRL_NBR = PRTHD.BYO_PO_LOC_SKU.PO_CTRL_NBR
)
AND (
PRTHD.BYO_PO.ACCT_PO_TYP_CD = PRTHD.BYO_PO_LOC_SKU.ACCT_PO_TYP_C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CD = 3 THEN 'RDCX'
END DSVC_TYP_DESC,
CE
WHEN P.DSVC_TYP_CD = 2
AND S.STR_NBR IS NOT NULL THEN 'Y'
ELSE 'N'
END RDC_AGG_PEG_FLG,
CE
WHEN P.DSVC_TYP_CD = 2
AND S.STR_NBR IS NOT NULL THEN P.ORD_REF_NBR
ELSE P.PO
END PO_NBR,
P.BYO_NBR,
P.PO_CTRL_NBR,
P.CRT_DT,
P.HOST_TYPE,
P.MVNDR_NBR,
P.MVNDR_NM,
CE
WHEN P.DSVC_TYP_CD = 2
AND S.STR_NBR IS NOT NULL THEN AR.MKT_DC_NBR
ELSE P.MKT_DC_NBR
END MKT_DC_NBR,
P.LOC_NBR,
COALESCE(SKU.MER_DEPT_NBR || '-' || D.SHRT_DEPT_NM, '') DEPT,
COALESCE(
SKU.MER_CLS_NBR || '-' || MC.SHRT_CLS_DESC,
''
) CLS,
COALESCE(
SKU.MER_SUB_CLS_NBR || '-' || MSC.SHRT_SUBCLS_DESC,
''
) SUB_CLS,
P.SKU_NBR,
P.ORD_QTY,
COALESCE(
SD.AVG_DC_LCOST_AMT * COALESCE(P.ORD_QTY, 0),
MSM.CURR_COST_AMT * COALESCE(P.ORD_QTY, 0)
) TOTAL_COST,
SD.AVG_DC_LCOST_AMT SD_COST,
MSM.CURR_COST_AMT STR_CURR_COST,
MSD.CURR_COST_AMT DC_CURR_COST,
P.PO_CMT_TXT,
P.RSN_CD
FROM PO_DETAILS P --SKU HIERARCHY INFO
LEFT JOIN PRTHD.SKU SKU ON SKU.SKU_NBR = P.SKU_NBR
LEFT JOIN PRTHD.DEPT D ON SKU.MER_DEPT_NBR = D.DEPT_NBR
LEFT JOIN PRTHD.MER_CLS MC ON MC.MER_DEPT_NBR = SKU.MER_DEPT_NBR
AND MC.MER_CLS_NBR = SKU.MER_CLS_NBR
LEFT JOIN PRTHD.MER_SUB_CLS MSC ON MSC.MER_DEPT_NBR = SKU.MER_DEPT_NBR
AND MSC.MER_CLS_NBR = SKU.MER_CLS_NBR
AND MSC.MER_SUB_CLS_NBR = SKU.MER_SUB_CLS_NBR
LEFT JOIN PRTHD.MVNDR_SKU_DC MSD ON MSD.MVNDR_NBR = P.MVNDR_NBR
AND MSD.SKU_NBR = P.SKU_NBR
AND MSD.DC_NBR = P.LOC_NBR
LEFT JOIN PRTHD.STR S ON S.STR_NBR = P.LOC_NBR
LEFT JOIN PRTHD.SKU_DC SD ON SD.DC_NBR = P.MKT_DC_NBR
AND SD.SKU_NBR = P.SKU_NBR
LEFT JOIN PRTHD.MVNDR_SKU_MKT MSM ON MSM.MVNDR_NBR = P.MVNDR_NBR
AND MSM.SKU_NBR = P.SKU_NBR
AND MSM.MKT_NBR = S.MKT_NBR
LEFT JOIN PRTHD.AGG_RQST AR ON AR.PO_NBR = P.ORD_REF_NBR
AND P.MVNDR_NBR = AR.MVNDR_NBR
AND SKU.MER_DEPT_NBR = AR.MER_DEPT_NBR
AND AR.ORD_DT >= P.CRT_DT

Oracle SQL - connecting 2 dates

I have the following code.
It has 2 dates:
1.
(case
when not trunc(iv.dated) is null then trunc(iv.dated) else trunc(iv1.dated)
end) date_stock_received
2.
trunc(dh.actshpdate) SHIP_DATE
Is there a way to join the dates so they show in one column?
select unique li.catnr, li.av_part_no,
(select sum(pl.qty_onhand) from part_loc pl where li.av_part_no = pl.part_no) qty_onhand,
(case
when not trunc(iv.dated) is null then trunc(iv.dated) else trunc(iv1.dated)
end) date_stock_received,
(case
when not sum(iv.quantity) is null then sum(iv.quantity) else sum(iv1.quantity)
end) qty_received,
dp.delqty, od.ord_extordnr, trunc(dh.actshpdate) SHIP_DATE
from leos_item li
LEFT JOIN scm_packtyp sp
ON li.packtyp = sp.packtyp
LEFT JOIN invtran_view_oes iv
ON li.av_part_no = iv.part_no
and (iv.transaction = 'NREC' and iv.location_no = ' RETURNS W')
LEFT JOIN invtran_view_oes iv1
on li.av_part_no = iv1.part_no
and (iv1.transaction = 'CORR+' and iv1.remark like 'STOCK FROM SP PALLET%')
LEFT JOIN oes_delsegview od
ON od.catnr = li.catnr
and od.prodtyp = li.prodtyp
and od.packtyp = li.packtyp
LEFT JOIN oes_dpos dp
ON od.ordnr = dp.ordnr
and od.posnr = dp.posnr
and od.segnr = dp.segnr
LEFT JOIN oes_dhead dh
on dp.dheadnr = dh.dheadnr
and dh.shpfromloc = 'W'
where li.cunr = '816900'
and substr(li.catnr,1,5) in ('RGMCD','RGJCD')
and li.item_type = 'FP'
and li.catnr = 'RGJCD221'
group by li.catnr, li.av_part_no, iv.dated, iv.quantity, iv1.dated, iv1.quantity, dp.delqty,
dp.ordnr, dp.posnr, dp.segnr, od.ord_extordnr, dh.actshpdate
order by li.av_part_no
Current result is ...
... what I would like to see is ...
Is this possible ?
The coalesce function might be what you want.
select trunc(coalesce(iv.dated, iv1.dated, dh.actshpdate)) theDateYouMightWant

How to change this 'Not In' Query to Left Join query

I've been trying to change this query. I don't want to use 'Not In' in this query. Can anybody help me how to change this query to left join query?
SELECT t.date,t.ticket,t.weight,t.Count, td.description
FROM tblticket t inner join tblticketdetails td on t.ticket = td.ticket
WHERE t.ticket NOT IN (SELECT r.Original_ticket from tblRenew R where isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid')
AND RTRIM(LTRIM(t.status)) = 'OPEN'
AND td.Type = 62
AND t.weight/t.Count >= 1000
AND t.date BETWEEN '2011-12-31' AND '2013-01-17'
Providing that you don't have multiple records in tblRenew for any ticket:
select
t.date, t.ticket, t.weight, t.Count, td.description
from
tblticket t
inner join tblticketdetails td on t.ticket = td.ticket
left join tblRenew R on isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid' and r.Original_ticket = t.ticket
where
r.Original_ticket is not null
and RTRIM(LTRIM(t.status)) = 'OPEN'
and td.Type = 62
and t.weight/t.Count >= 1000
and t.date BETWEEN '2011-12-31' AND '2013-01-17'
SELECT t.date, t.ticket,t.weight, t.Count, td.description
FROM tblticket t inner join
tblticketdetails td
on t.ticket = td.ticket left outer join
(SELECT r.Original_ticket
from tblRenew R
where isnull(r.new_ticket,'') = '' and
r.transaction_status = 'valid'
) v
on t.ticket = v.Original_ticket
WHERE t.ticket NOT IN (SELECT r.Original_ticket from tblRenew R where isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid')
AND RTRIM(LTRIM(t.status)) = 'OPEN'
AND td.Type = 62
AND t.weight/t.Count >= 1000
AND t.date BETWEEN '2011-12-31' AND '2013-01-17'
and v.original_tiket is null