SQL Server Left Join issue - sql

When I execute this:
SELECT i.descricao,
sii.quantidade quantidade_inicial,
sii.quantidade * i.valor valor_inicial
FROM itensvalor i
LEFT JOIN saldoinicialprodutositens sii
ON i.id = sii.iditemvalor
AND Isnull(sii.datahoraexclusao, '') = ''
WHERE sii.idSaldoInicialProduto IN (SELECT id FROM saldoInicialProdutos WHERE idmovimentodiario = 76)
It returns this:
And when I execute this:
SELECT i.descricao,
sfi.quantidade quantidade_final,
sfi.quantidade * i.valor valor_final
FROM itensvalor i
LEFT JOIN saldofinalprodutositens sfi
ON i.id = sfi.iditemvalor
AND Isnull(sfi.datahoraexclusao, '') = ''
WHERE sfi.idSaldoFinalProduto IN (SELECT id FROM saldoFinalProdutos WHERE idmovimentodiario = 76)
It returns this: (with the R$100,00)
But, finally, when I try to join the 2 tables, the value that refers to R$100,00 not appears.
I'm trying to join using this query:
SELECT i.descricao,
sii.quantidade quantidade_inicial,
sii.quantidade * i.valor valor_inicial,
sfi.quantidade quantidade_final,
sfi.quantidade * i.valor valor_final
FROM itensvalor i
LEFT JOIN saldofinalprodutositens sfi
ON i.id = sfi.iditemvalor
AND Isnull(sfi.datahoraexclusao, '') = ''
LEFT JOIN saldoinicialprodutositens sii
ON i.id = sii.iditemvalor
AND Isnull(sii.datahoraexclusao, '') = ''
WHERE sii.idSaldoInicialProduto IN (SELECT id FROM saldoInicialProdutos WHERE idmovimentodiario = 76)
AND sfi.idSaldoFinalProduto IN (SELECT id FROM saldoFinalProdutos WHERE idmovimentodiario = 76)

You need to move your WHERE inside the ON clause for that to work:
SELECT i.descricao,
sii.quantidade quantidade_inicial,
sii.quantidade * i.valor valor_inicial,
sfi.quantidade quantidade_final,
sfi.quantidade * i.valor valor_final
FROM itensvalor i
LEFT JOIN saldofinalprodutositens sfi
ON i.id = sfi.iditemvalor
AND Isnull(sfi.datahoraexclusao, '') = ''
and sfi.idSaldoFinalProduto IN (SELECT id FROM saldoFinalProdutos WHERE idmovimentodiario = 76)
LEFT JOIN saldoinicialprodutositens sii
ON i.id = sii.iditemvalor
AND Isnull(sii.datahoraexclusao, '') = ''
and sii.idSaldoInicialProduto IN (SELECT id FROM saldoInicialProdutos WHERE idmovimentodiario = 76)
Otherwise, your sfi.idSaldoFinalProduto is null, and the row is omited.

Related

'Order By' with 'Union' and 'GroupBy' (Oracle)

Good Morning. I have a query that has Group By and Union tables. The problem is when I try to order the final result. I'm always getting an error:
SELECT {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
FROM {Account}
INNER JOIN {ApprovalWorkUnitDetail} ON {ApprovalWorkUnitDetail}.[AccountID] = {Account}.[Id]
INNER JOIN {Issue} ON {Issue}.[Id] = {ApprovalWorkUnitDetail}.[IssueID]
INNER JOIN {ServiceRequest} ON {ServiceRequest}.[Id] = {Issue}.[ServiceRequestId]
INNER JOIN {ServiceRequestProduct} ON {ServiceRequestProduct}.[ServiceRequestId] = {ServiceRequest}.[Id]
INNER JOIN {Product} ON {Product}.[Id] = {ServiceRequestProduct}.[ProductId]
LEFT JOIN {Application} ON {Application}.[Id] = {Account}.[ApplicationID]
WHERE ({ApprovalWorkUnitDetail}.[ContractID] = #ContractID)
AND ({ServiceRequest}.[Id] = #ServiceRequestID OR #ServiceRequestID = #NullIdentifier)
AND ({Product}.[Id] = #ProductID OR #ProductID = #NullIdentifier)
AND ({ApprovalWorkUnitDetail}.[MonthIdentifier] = #MonthIdentifier)
AND ({ApprovalWorkUnitDetail}.[YearIdentifier] = #YearIdentifier)
AND ({Issue}.[PaymentStatusID] = #PaymentStatusApprovedID)
UNION
SELECT {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
FROM {Account}
INNER JOIN {ApprovalRefinementDetail} ON {ApprovalRefinementDetail}.[AccountID] = {Account}.[Id]
INNER JOIN {Worklog} ON {Worklog}.[Id] = {ApprovalRefinementDetail}.[WorklogID]
INNER JOIN {Issue} ON {Worklog}.[IssueId] = {Issue}.[Id]
INNER JOIN {ServiceRequest} ON {ServiceRequest}.[Id] = {Issue}.[ServiceRequestId]
INNER JOIN {ServiceRequestProduct} ON {ServiceRequestProduct}.[ServiceRequestId] = {ServiceRequest}.[Id]
INNER JOIN {Product} ON {Product}.[Id] = {ServiceRequestProduct}.[ProductId]
LEFT JOIN {Application} ON {Application}.[Id] = {Account}.[ApplicationID]
WHERE ({ApprovalRefinementDetail}.[ContractID] = #ContractID)
AND ({Product}.[Id] = #ProductID OR #ProductID = #NullIdentifier)
AND ({ServiceRequest}.[Id] = #ServiceRequestID OR #ServiceRequestID = #NullIdentifier)
AND ({ApprovalRefinementDetail}.[MonthIdentifier] = #MonthIdentifier)
AND ({ApprovalRefinementDetail}.[YearIdentifier] = #YearIdentifier)
AND ({Worklog}.[PaymentStatusID] = #PaymentStatusApprovedID)
GROUP BY {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
ORDER BY {Account}.[AccountName]
I don't know why I am always getting error ORA-00904 and it indicates that AccountName is an invalid identifier.
I am using ORACLE database.
Many thanks
I would recommend moving your union query to a subquery, and sorting in the outer scope:
select *
from (
-- your big query
) t
order by accountname

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.MVNDR_NM,
PRTHD.BYO_PO.MVNDR_NBR,
PRTHD.BYO_PO.OCYC_SOQ_RSN_CD AS RSN_CD
FROM ((PRTHD.BYO_PO AS BP INNER JOIN PRTHD.BYO_PO_LOC_SKU
ON (PRTHD.B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S 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_T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RTHD.BYO_PO.BYO_NBR = PRTHD.BYO_PO_LOC_SKU.BYO_NBR
)
)
INNER JOIN PRTHD.SKU ON PRTHD.BYO_PO_LOC_SKU.SKU_NBR = PRTHD.SKU.SKU_NBR
)
INNER JOIN PRTHD.BYO_PO_SKU ON (PRTHD.SKU.SKU_NBR = PRTHD.BYO_PO_SKU.SKU_NBR)
AND (
PRTHD.BYO_PO_LOC_SKU.BYO_NBR = PRTHD.BYO_PO_SKU.BYO_NBR
)
AND (
PRTHD.BYO_PO_LOC_SKU.MKT_DC_IND = PRTHD.BYO_PO_SKU.MKT_DC_IND
)
AND (
PRTHD.BYO_PO_LOC_SKU.MKT_DC_NBR = PRTHD.BYO_PO_SKU.MKT_DC_NBR
)
AND (
PRTHD.BYO_PO_LOC_SKU.ACCT_PO_TYP_CD = PRTHD.BYO_PO_SKU.ACCT_PO_TYP_CD
)
AND (
PRTHD.BYO_PO_LOC_SKU.PO_CTRL_NBR = PRTHD.BYO_PO_SKU.PO_CTRL_NBR
)
AND (
PRTHD.BYO_PO_LOC_SKU.ORD_SEQ_NBR = PRTHD.BYO_PO_SKU.ORD_SEQ_NBR
)
AND (
PRTHD.BYO_PO_LOC_SKU.PO_LINE_NBR = PRTHD.BYO_PO_SKU.PO_LINE_NBR
)
LEFT JOIN PRTHD.MVNDR M ON M.MVNDR_NBR = PRTHD.BYO_PO.MVNDR_NBR
INNER JOIN RQST_TYPE RT ON TRIM(SUBSTR(BP.PO_CMT_TXT, 6, 11)) = RT.RQST_ID
) A
)
Select P.RQST_ID,
P.DSVC_TYP_CD,
CE
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 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

How to use SQL Conditional statements in SQL

I'm working on a BIRT Reporting. What I need to do is, If the Column1 value is Approved, Copy Column 2 value to Column 3 else null
SELECT pr.prnum,prline.prlinenum,prline.itemnum,prline.description,prline.orderqty,prline.ponum,pr.status as "PRSTATUS",
postatusappr.changedate as "POAPPRDATE", matrectrans.actualdate as "ACTUALDELIVDATE", prstatuswappr.changedate as "PRCREATED",
prstatusappr.changedate as "PRAPPRDATE", days (current date) - days(date(prstatusappr.changedate)) as "NOOFDAYSAFTERPRAPPR",
INTEGER(days (current date) - days(date(prstatusappr.changedate)))/7 as "NOOFWEEKSAFTERPRAPPR",
INTEGER(days (current date) - days(date(postatusappr.changedate)))/7 as "NOOFWEEKSAFTERPOAPPR" FROM pr pr
LEFT JOIN prline prline ON prline.prnum = pr.prnum AND prline.siteid = pr.siteid
LEFT JOIN poline poline ON poline.ponum = prline.ponum AND poline.siteid = pr.siteid
LEFT JOIN postatus postatusappr ON postatusappr.ponum = poline.ponum AND postatusappr.siteid = pr.siteid AND postatusappr.status = 'APPR'
LEFT JOIN matrectrans matrectrans ON matrectrans.ponum = poline.ponum AND matrectrans.polinenum = poline.polinenum AND matrectrans.positeid = pr.siteid AND matrectrans.issuetype='RECEIPT' AND matrectrans.status = 'COMP'
LEFT JOIN prstatus prstatuswappr ON prstatuswappr.prnum = pr.prnum AND prstatuswappr.status = 'WAPPR' AND prstatuswappr.siteid = pr.siteid
LEFT JOIN prstatus prstatusappr ON prstatusappr.prnum = pr.prnum AND prstatusappr.status = 'APPR' AND prstatusappr.siteid = pr.siteid
where prline.itemnum is not null;
You may try doing an UPDATE with a CASE expression, something like this:
UPDATE yourTable
SET Column3 = CASE WHEN Column1 = 'Approved' THEN Column2 ELSE NULL END;

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.

ORA-01799: a column may not be outer-joined to a subquery with insert statement

I am trying to create this procedure:
CREATE OR REPLACE PROCEDURE USP_DAILY_REP_REG_REFRESH
IS
BEGIN
SAVEPOINT start_tran;
EXECUTE immediate 'truncate table rep_fir_acts';
-- Insert Query
INSERT
INTO rep_fir_acts
(
PS_CD,
LANG_CD,
FIR_REG_NUM,
FIR_DT,
ACT_CD,
PEND_INV_FIR_NUM,
FINAL_REP_SRNO,
CHARGESHEET_DT,
INVTG_STATUS_CD,
FR_TYPE_CD,
FINAL_REPORT_TYPE_CD,
PEND_TRIAL_FIR,
DISPOSAL_NATURE_CD,
ACTION_TAKEN_CD,
FIR_STATUS
)
SELECT DISTINCT F.PS_CD,
F.LANG_CD,
F.FIR_REG_NUM,
F.REG_DT FIR_DT,
C.ACT_CD ,
fnl.FIR_REG_NUM PEND_INV_FIR_NUM,
fnl.FINAL_REP_SRNO ,
fnl.CHARGESHEET_DT,
fnl.INVTG_STATUS_CD,
fnl.FR_TYPE_CD,
fnl.FINAL_REPORT_TYPE_CD ,
dis.FIR_REG_NUM PEND_TRIAL_FIR,
dis.DISPOSAL_NATURE_CD ,
f.ACTION_TAKEN_CD,
f.FIR_STATUS
FROM t_fir_registration f
INNER JOIN t_crime_act_section c
ON f.fir_reg_num = c.fir_reg_num
AND f.lang_cd = c.lang_cd
INNER JOIN t_crime_detail cd
ON c.fir_reg_num = cd.fir_reg_num
AND c.lang_cd = cd.lang_cd
AND c.CRM_DETAIL_SRNO = cd.CRM_DETAIL_SRNO
AND cd.RECORD_STATUS = 'C'
LEFT JOIN t_final_report fnl
ON f.fir_reg_num = fnl.fir_reg_num
AND f.lang_cd = fnl.lang_cd
AND IS_CHARGESHEET_ORIGINAL = 'Y'
AND fnl.final_rep_srno IN
(SELECT MAX(final_rep_srno)
FROM t_final_report fn
WHERE f.fir_reg_num = fn.fir_reg_num
AND f.lang_cd = fn.lang_cd
AND fn.IS_CHARGESHEET_ORIGINAL = 'Y'
)
LEFT JOIN t_court_disposal dis
ON f.fir_reg_num = dis.fir_reg_num
AND dis.DISPOSAL_SRNO IN
(SELECT MAX(DISPOSAL_SRNO)
FROM t_court_disposal cdis
WHERE dis.fir_reg_num = cdis.fir_reg_num
)
WHERE c.act_cd <> 0
AND f.fir_status NOT IN(2,20,104,103);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO start_tran;
RAISE;
END;
When I try to run this I get:
PL/SQL: SQL Statement ignored
ORA-01799: a column may not be outer-joined to a subquery with insert
statement
How can I resolve this problem?
You need to convert what you're left joining into subqueries, rather than having the subquery in the join conditions. E.g., something like:
SELECT DISTINCT f.ps_cd,
f.lang_cd,
f.fir_reg_num,
f.reg_dt fir_dt,
c.act_cd,
fnl.fir_reg_num pend_inv_fir_num,
fnl.final_rep_srno,
fnl.chargesheet_dt,
fnl.invtg_status_cd,
fnl.fr_type_cd,
fnl.final_report_type_cd,
dis.fir_reg_num pend_trial_fir,
dis.disposal_nature_cd,
f.action_taken_cd,
f.fir_status
FROM t_fir_registration f
INNER JOIN t_crime_act_section c
ON f.fir_reg_num = c.fir_reg_num
AND f.lang_cd = c.lang_cd
INNER JOIN t_crime_detail cd
ON c.fir_reg_num = cd.fir_reg_num
AND c.lang_cd = cd.lang_cd
AND c.crm_detail_srno = cd.crm_detail_srno
AND cd.record_status = 'C'
LEFT JOIN (SELECT fir_reg_num,
lang_cd,
final_rep_srno,
chargesheet_dt,
invtg_status_cd,
fr_type_cd,
final_report_type_cd
FROM (SELECT fnl1.*,
MAX(fnl1.final_rep_srno) OVER (PARTITION BY fnl1.fir_reg_num, fnl1.lang_cd) max_final_rep_srno
FROM t_final_report fnl1
WHERE fnl1.is_chargesheet_original = 'Y')
WHERE final_rep_srno = max_final_rep_srno) fnl
ON f.fir_reg_num = fnl.fir_reg_num
AND f.lang_cd = fnl.lang_cd
LEFT JOIN (SELECT fir_reg_num,
disposal_nature_cd
FROM (SELECT dis1.*,
MAX(disposal_srno) OVER (PARTITION BY dis1.fir_reg_num) max_disposal_srno
FROM t_court_disposal dis1)
WHERE disposal_srno = max_disposal_srno) dis
ON f.fir_reg_num = dis.fir_reg_num
WHERE c.act_cd <> 0
AND f.fir_status NOT IN (2, 20, 104, 103);