I have a view with the below SQL working and pulling data in Oracle SQL developer, but when I import this view in to the OBIEE repository and try to view data I get the error above. Originally the alias was "Segment" but I thought it might have been a Oracle reserved word. I changed it to "Segment_ID" and then to just "SEG" and neither of them have worked.
CREATE OR REPLACE FORCE VIEW "EPM_FCM_STG"."ARM_PROCESS_MONITOR" ("PERIOD", "ACCOUNT_ID", "Company", "Account", "DESCRIPTION", "SRC_SYS_BAL_BUCKET", "BAL_EXP_BUCKET", "SUBSYS_BAL_BUCKET", "ADJ_SUBSYS_BUCKET", "PREPARER_FREQUENCY", "END_DATE_ACTUAL", "PREPARER", "REVIEWER_1", "REVIEWER_2", "NORMAL_BALANCE", "Seg") AS
SELECT R_PeriodEO.PERIOD_NAME AS "PERIOD",
ReconciliationEO.RECONCILIATION_ACCOUNT_ID AS "ACCOUNT_ID",
R_AttributeValueEO1A.VALUE_TEXT AS "Company",
R_AttributeValueEO2A.VALUE_TEXT AS "Account",
ReconciliationEO.RECONCILIATION_DESCRIPTION AS "DESCRIPTION",
R_BalanceSummaryEO3A.AMOUNT AS "SRC_SYS_BAL_BUCKET",
R_TransactionSummaryEO4A.AMOUNT AS "BAL_EXP_BUCKET",
R_BalanceSummaryEO5A.AMOUNT AS "SUBSYS_BAL_BUCKET",
R_TransactionSummaryEO6A.AMOUNT AS "ADJ_SUBSYS_BUCKET",
R_FrequencyEO8A.FREQUENCY_NAME AS "PREPARER_FREQUENCY",
TO_CHAR(ReconciliationEO.ACTUAL_END_DATE, 'YYYY-MM-DD"T"hh:mi:ss') AS "END_DATE_ACTUAL",
(
(SELECT
CASE
WHEN FCM_USERS.FIRST_NAME IS NULL
AND FCM_USERS.LAST_NAME IS NULL
THEN FCM_USERS.USER_LOGIN
ELSE FCM_USERS.FIRST_NAME
|| ' '
|| FCM_USERS.LAST_NAME
END
FROM FCM_USERS
WHERE FCM_USERS.USER_ID = (COALESCE(R_PreparerEO.ACTIVE_USER_ID, R_PreparerEO.USER_ID))
)) AS "PREPARER",
(
(SELECT
CASE
WHEN FCM_USERS.FIRST_NAME IS NULL
AND FCM_USERS.LAST_NAME IS NULL
THEN FCM_USERS.USER_LOGIN
ELSE FCM_USERS.FIRST_NAME
|| ' '
|| FCM_USERS.LAST_NAME
END
FROM FCM_USERS
WHERE FCM_USERS.USER_ID = (COALESCE(R_ReviewerEO1R.ACTIVE_USER_ID, R_ReviewerEO1R.USER_ID))
)) AS "REVIEWER_1",
(
(SELECT
CASE
WHEN FCM_USERS.FIRST_NAME IS NULL
AND FCM_USERS.LAST_NAME IS NULL
THEN FCM_USERS.USER_LOGIN
ELSE FCM_USERS.FIRST_NAME
|| ' '
|| FCM_USERS.LAST_NAME
END
FROM FCM_USERS
WHERE FCM_USERS.USER_ID = (COALESCE(R_ReviewerEO2R.ACTIVE_USER_ID, R_ReviewerEO2R.USER_ID))
)) AS "REVIEWER_2",
CASE ReconciliationEO.NORMAL_BALANCE
WHEN 'C'
THEN 'CREDIT'
ELSE
CASE ReconciliationEO.NORMAL_BALANCE
WHEN 'D'
THEN 'DEBIT'
ELSE
CASE ReconciliationEO.NORMAL_BALANCE
WHEN 'E'
THEN 'EITHER_DEBIT_OR_CREDIT'
ELSE 'N/A'
END
END
END AS "NORMAL_BALANCE",
R_AttributeValueEO15A.VALUE_TEXT AS "Seg"
FROM ARM_RECONCILIATIONS ReconciliationEO
LEFT OUTER JOIN ARM_PERIODS R_PeriodEO
ON (ReconciliationEO.PERIOD_ID = R_PeriodEO.PERIOD_ID)
LEFT OUTER JOIN ARM_ATTRIBUTE_VALUES R_AttributeValueEO1A
ON ReconciliationEO.RECONCILIATION_ID = R_AttributeValueEO1A.OBJECT_ID
AND (R_AttributeValueEO1A.ATTRIBUTE_ID = 100000000027005)
LEFT OUTER JOIN ARM_ATTRIBUTE_VALUES R_AttributeValueEO2A
ON ReconciliationEO.RECONCILIATION_ID = R_AttributeValueEO2A.OBJECT_ID
AND (R_AttributeValueEO2A.ATTRIBUTE_ID = 100000000027021)
LEFT OUTER JOIN ARM_BALANCE_SUMMARIES R_BalanceSummaryEO3A
ON ReconciliationEO.RECONCILIATION_ACCOUNT_ID = R_BalanceSummaryEO3A.PROFILE_ID
AND R_BalanceSummaryEO3A.PERIOD_ID = ReconciliationEO.PERIOD_ID
AND (R_BalanceSummaryEO3A.BUCKET_ID = 100003
AND R_BalanceSummaryEO3A.BALANCE_TYPE = 1
AND 1 = 1)
LEFT OUTER JOIN ARM_TRANSACTION_SUMMARIES R_TransactionSummaryEO4A
ON ReconciliationEO.RECONCILIATION_ID = R_TransactionSummaryEO4A.RECONCILIATION_ID
AND (R_TransactionSummaryEO4A.CURRENCY_BUCKET_ID = 100003
AND R_TransactionSummaryEO4A.TRANSACTION_TYPE = 'BEX'
AND 1 = 1)
LEFT OUTER JOIN ARM_BALANCE_SUMMARIES R_BalanceSummaryEO5A
ON ReconciliationEO.RECONCILIATION_ACCOUNT_ID = R_BalanceSummaryEO5A.PROFILE_ID
AND R_BalanceSummaryEO5A.PERIOD_ID = ReconciliationEO.PERIOD_ID
AND (R_BalanceSummaryEO5A.BUCKET_ID = 100003
AND R_BalanceSummaryEO5A.BALANCE_TYPE = 2
AND 1 = 1)
LEFT OUTER JOIN ARM_TRANSACTION_SUMMARIES R_TransactionSummaryEO6A
ON ReconciliationEO.RECONCILIATION_ID = R_TransactionSummaryEO6A.RECONCILIATION_ID
AND (R_TransactionSummaryEO6A.CURRENCY_BUCKET_ID = 100003
AND R_TransactionSummaryEO6A.TRANSACTION_TYPE = 'SUB'
AND 1 = 1)
LEFT OUTER JOIN ARM_ACCESS R_PreparerEO
ON ReconciliationEO.RECONCILIATION_ID = R_PreparerEO.OBJECT_ID
AND (R_PreparerEO.ACCESS_TYPE = 'P')
LEFT OUTER JOIN ARM_FREQUENCIES R_FrequencyEO8A
ON (R_PreparerEO.FREQUENCY_ID = R_FrequencyEO8A.FREQUENCY_ID)
LEFT OUTER JOIN ARM_ACCESS R_ReviewerEO1R
ON ReconciliationEO.RECONCILIATION_ID = R_ReviewerEO1R.OBJECT_ID
AND (R_ReviewerEO1R.ACCESS_TYPE = 'R'
AND R_ReviewerEO1R.ACCESS_ORDER = 1)
LEFT OUTER JOIN ARM_ACCESS R_ReviewerEO2R
ON ReconciliationEO.RECONCILIATION_ID = R_ReviewerEO2R.OBJECT_ID
AND (R_ReviewerEO2R.ACCESS_TYPE = 'R'
AND R_ReviewerEO2R.ACCESS_ORDER = 2)
LEFT OUTER JOIN ARM_ATTRIBUTE_VALUES R_AttributeValueEO15A
ON ReconciliationEO.RECONCILIATION_ID = R_AttributeValueEO15A.OBJECT_ID
AND (R_AttributeValueEO15A.ATTRIBUTE_ID = 100000000003025)
WHERE ReconciliationEO.PERIOD_ID IS NOT NULL;
The error I get is:
Related
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_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= 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 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 || RI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YO_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
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;
I have this basic query, bringing back configuration information. Each MatrixCellID is an individual ticket, and each can have multiple StatisticalGroupCodes and StatisicalGroupDescriptions. Currently each MatrixCellID appears multiple times because of its multiple StatisicalGroupCodes. I would like the Code and Description columns to show all in one column, comma separated.
This is my query:
select
cmc.MatrixSheetId,
CMS.MatrixSheetName,
cmc.MatrixCellId,
CMC.Price,
CMC.PriceListId,
CPL.PriceListName,
CPT.Description as PriceTable,
case when CMC.Code <> '' then cmc.code else 'EMPTY' end AS TicketCode,
case when CMC.Name <> '' then cmc.name else 'EMPTY' END AS TicketName,
case when CMC.Description <> '' then cmc.description else 'EMPTY' END AS TicketDescription,
case when CMC.Description2 <> '' then cmc.description2 else 'EMPTY' end AS AdditionalTicketDescription,
CASE WHEN CMCI.AccountMandatory = 1 THEN 'YES' else 'NO' end AS AccountMandatory,
CASE WHEN CDC.Description IS NULL THEN 'NONE' ELSE CDC.Description END AS AccountCategory,
CDT.DocTemplateName AS PrintTemplate,
CTP.Description as TaxPackage,
CT.TaxName,
CASE when CMC.PriceType = 0 then 'Fixed' else 'Variable' end as PriceType,
CCC.CostCenterDescription,
CCC.CostCenterCode,
CCC.CostCenterAK,
CSG.StatisticalGroupCode,
CSG.StatisticalGroupDescription
from
CNF_MatrixCell CMC
inner join CNF_MatrixSheet CMS on CMC.MatrixSheetId = CMS.MatrixSheetId
inner join CNF_PriceList CPL on CMC.PriceListId = CPL.PriceListId
INNER JOIN CNF_MatrixCellInfo CMCI on CMC.MatrixCellId = CMCI.MatrixCellId
left join CNF_DmgCategory CDC on CMCI.AccountDmgCatId = CDC.DmgCategoryId
left join CNF_DocTemplate CDT on CMC.DocTemplateId = CDT.DocTemplateId
LEFT join CNF_TaxPackage CTP on CMC.TaxPackageId = CTP.TaxPackageId
LEFT join CNF_Tax2Package CT2P on CTP.TaxPackageId = CT2P.TaxPackageId
LEFT JOIN CNF_Tax CT on CT2P.TaxId = CT.TaxId
LEFT JOIN CNF_CostCenter_Validity CCCV on CMC.MatrixCellId = cccv.MatrixCellId
LEFT JOIN CNF_CostCenter CCC on CCCV.CostCenterId = CCC.CostCenterId
inner join CNF_PriceTable CPT on CMC.PriceTableId = CPT.PriceTableId
LEFT JOIN CNF_StatisticalGroupValidity CSGV on CMC.MatrixCellId = CSGV.MatrixCellId
LEFT JOIN CNF_StatisticalGroup CSG on CSGV.StatisticalGroupId = CSG.StatisticalGroupId
WHERE CMC.Enabled = 1
AND CCC.Enabled = 1
AND CPT.Enabled = 1
AND CMS.Enabled = 1
ORDER BY
CMS.MatrixSheetId,
CMC.MatrixCellId,
CMC.Code
I have tried to use Stuff and XML Path, but cannot get it work correctly with my joins.
Thank you in advance.
You can use this.
select
cmc.MatrixSheetId,
CMS.MatrixSheetName,
cmc.MatrixCellId,
CMC.Price,
CMC.PriceListId,
CPL.PriceListName,
CPT.Description as PriceTable,
case when CMC.Code <> '' then cmc.code else 'EMPTY' end AS TicketCode,
case when CMC.Name <> '' then cmc.name else 'EMPTY' END AS TicketName,
case when CMC.Description <> '' then cmc.description else 'EMPTY' END AS TicketDescription,
case when CMC.Description2 <> '' then cmc.description2 else 'EMPTY' end AS AdditionalTicketDescription,
CASE WHEN CMCI.AccountMandatory = 1 THEN 'YES' else 'NO' end AS AccountMandatory,
CASE WHEN CDC.Description IS NULL THEN 'NONE' ELSE CDC.Description END AS AccountCategory,
CDT.DocTemplateName AS PrintTemplate,
CTP.Description as TaxPackage,
CT.TaxName,
CASE when CMC.PriceType = 0 then 'Fixed' else 'Variable' end as PriceType,
CCC.CostCenterDescription,
CCC.CostCenterCode,
CCC.CostCenterAK,
STUFF(StatGrpCode.StatGroupCodes,1,1,'') StatGroupCodes,
STUFF(StatGrpCodeDesc.StatGroupDescription,1,1,'') StatGroupDescription
from
CNF_MatrixCell CMC
inner join CNF_MatrixSheet CMS on CMC.MatrixSheetId = CMS.MatrixSheetId
inner join CNF_PriceList CPL on CMC.PriceListId = CPL.PriceListId
INNER JOIN CNF_MatrixCellInfo CMCI on CMC.MatrixCellId = CMCI.MatrixCellId
left join CNF_DmgCategory CDC on CMCI.AccountDmgCatId = CDC.DmgCategoryId
left join CNF_DocTemplate CDT on CMC.DocTemplateId = CDT.DocTemplateId
LEFT join CNF_TaxPackage CTP on CMC.TaxPackageId = CTP.TaxPackageId
LEFT join CNF_Tax2Package CT2P on CTP.TaxPackageId = CT2P.TaxPackageId
LEFT JOIN CNF_Tax CT on CT2P.TaxId = CT.TaxId
LEFT JOIN CNF_CostCenter_Validity CCCV on CMC.MatrixCellId = cccv.MatrixCellId
LEFT JOIN CNF_CostCenter CCC on CCCV.CostCenterId = CCC.CostCenterId
inner join CNF_PriceTable CPT on CMC.PriceTableId = CPT.PriceTableId
LEFT JOIN CNF_StatisticalGroupValidity CSGV on CMC.MatrixCellId = CSGV.MatrixCellId
OUTER APPLY
( SELECT ', ' + CSG.StatisticalGroupCode
FROM CNF_StatisticalGroup CSG
WHERE CSGV.StatisticalGroupId = CSG.StatisticalGroupId FOR XML PATH('') ) StatGrpCode (StatGroupCodes)
OUTER APPLY
( SELECT ', ' + CSG.StatisticalGroupDescription
FROM CNF_StatisticalGroup CSG
WHERE CSGV.StatisticalGroupId = CSG.StatisticalGroupId FOR XML PATH('') ) StatGrpCodeDesc (StatGroupDescription)
WHERE CMC.Enabled = 1
AND CCC.Enabled = 1
AND CPT.Enabled = 1
AND CMS.Enabled = 1
ORDER BY
CMS.MatrixSheetId,
CMC.MatrixCellId,
CMC.Code
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
A bit new to Oracle specific SQL syntax, more familiar with MS SQL Server.
I'm having trouble with a join.
Specifically, the join is supposed to allow me to return the most recent date from a table. (In SQL Server I would just put select top 1 date order by date---with oracle it is confusing!)
Here is my entire query, I'm trying to return the max date for each of the date columns in the select statement with the MAX( prefix. (I know the max prefix isn't the correct syntax to get what I'm after, I'm just listing that here so you know which dates I need max date back on..) How do I get the most current date back for those columns?:
SELECT DISTINCT
req.LCR_REQUEST_ID "Request_ID"
,req.MATTER_ID AS "MatterID_lcr"
,req.CUSTOMER_NAME AS "AccountName_lcr"
,req.STATUS AS "CurrentContractStatus_lcr"
,MAX(HIS.RECORDED_DATE) AS "CurrentStatus_ChangeDate"
--,HIS.STATUS AS "ContractStatus_Historic_lcr"
,MAX(HIS_SUB.RECORDED_DATE) AS "Submitted_Status_Date"
--,HIS_NA.RECORDED_DATE AS "NotAssigned_Status_Date"
,MAX(HIS_AtA.RECORDED_DATE) AS "AssignedToAttorney_Status_Date"
,MAX(HIS_SCIN.RECORDED_DATE) AS "InNegotiation_Status_Date"
,MAX(HIS_EXE.RECORDED_DATE) AS "Executed_Status_Date"
,MAX(HIS_CONV.RECORDED_DATE) AS "ConverteToAmend_Status_Date"
,MAX(HIS_DEAD.RECORDED_DATE) AS "Dead_Status_Date"
,MAX(HIS_COMP.RECORDED_DATE) AS "Completed_Status_Date"
,MAX(HIS_EXP.RECORDED_DATE) AS "Expired_Status_Date"
,CASE
WHEN req.DEALSIZE = 0 THEN ''
WHEN req.DEALSIZE = 1 THEN 'Less Than 100K'
when req.DEALSIZE = 2 THEN '100K-500K'
when req.DEALSIZE = 3 THEN '500K-1M'
when req.DEALSIZE = 4 THEN '1M-10M'
when req.DEALSIZE = 5 THEN '>10M'
END
AS "DealSize_lcr"
,cm_out.PTY_NAME AS "AccountName_cm"
,cm_out.PTY_ID AS "PTYID_CM"
,req.customer_id "PtyID_lcr"
,cm_out.EFFECTIVE_DATE "EffDate_lcr"
,cm_out.INITIALTERM_END_DATE "CPEDate_lcr"
,cm_out.CONTRACT_TERMINATION_DATE "FCTDate_lcr"
,cm_out.TERM_NOTICE_NONCOMP_REASON "ReasonForTermination_lcr"
,cm_out.TERM_TYPE "TermType_lcr"
,cm_out.INITIALTERM_DURATION "InitialTerm_lcr"
,cm_out.INVOICE_TIMEMEAS "InitialTermTymeMeas_lcr"
,cm_out.NUMBER_OF_RENEWALS "NumberOfRenewals_lcr"
,req.AMENDMENT "ParentMatterID_lcr"
,prod.rpg "RPG_lcr"
,prod.SALES_PRODUCT_NAME "Product_lcr"
,prod.CONTRACT_TYPE "ContractType_lcr"
,req.CONTRACT_ROLE "ContractRole_lcr"
,cm_out.METADATA_FLAG "MetaFlag_lcr"
,CASE
WHEN cm_out.DATA_AUTO_IMPORT_YN = 1 THEN 'YES'
WHEN cm_out.REQUESTOR = 'Imported Record' THEN 'YES'
ELSE 'NO'
END
"DataImport_lcr"
,CASE act.actionstatus
WHEN 'Cancel Date Update' THEN 'Yes'
ELSE NULL
END "ActionToCancelAutoDate_lcr"
,req_emp.FIRST_NAME || ' ' || req_emp.LAST_NAME "Requestor_lcr"
,att_emp.FIRST_NAME || ' ' || att_emp.LAST_NAME "Attorney_lcr"
,req.REQUESTOR_DEPARTMENT "Requestor_Department_lcr"
,req.AGREEMENT_TITLE "AgreementTitle_lcr"
,req.DATE_RECORDED
FROM LCR_REQUEST REQ
LEFT JOIN LCR_ACTION ACT ON req.MATTER_ID = act.MATTER_ID
LEFT JOIN CM_CONTRACT_OUTBOUND cm_out ON cm_out.MATTER_ID = NVL(req.MATTER_ID,req.AMENDMENT)
LEFT JOIN LCR_REQUEST_PRODUCT prod_REQ ON prod_req.LCR_REQUEST_ID = req.LCR_REQUEST_ID
LEFT JOIN LCR_PRODUCT PROD ON prod.PRODUCT_ID = prod_req.PRODUCT_ID
LEFT JOIN LCR_EMPLOYEE att_emp ON req.ASSIGNED_TO = att_emp.ORACLE_PERSON_ID
LEFT JOIN LCR_EMPLOYEE req_emp ON req.REQUESTED_BY_ID = req_emp .ORACLE_PERSON_ID
LEFT JOIN LCR_STATUS_HISTORY HIS ON HIS.LCR_REQUEST_ID = req.LCR_REQUEST_ID AND ((HIS.STATUS = REQ.STATUS) OR (HIS.STATUS = 'Assigned to Attorney' AND REQ.STATUS = 'Assigned To Attorney'))
--LEFT JOIN LCR_STATUS_HISTORY HIS_NA ON req.LCR_REQUEST_ID = HIS_NA.LCR_REQUEST_ID AND HIS_NA.STATUS = 'Not Assigned'
LEFT JOIN LCR_STATUS_HISTORY HIS_AtA ON req.LCR_REQUEST_ID = HIS_AtA.LCR_REQUEST_ID AND HIS_AtA.STATUS = 'Assigned to Attorney'
LEFT JOIN LCR_STATUS_HISTORY HIS_SUB ON req.LCR_REQUEST_ID = HIS_SUB.LCR_REQUEST_ID AND HIS_SUB.STATUS = 'Submitted'
LEFT JOIN LCR_STATUS_HISTORY HIS_SCIN ON req.LCR_REQUEST_ID = HIS_SCIN.LCR_REQUEST_ID AND HIS_SCIN.STATUS = 'Submission Complete-In Negotiation'
LEFT JOIN LCR_STATUS_HISTORY HIS_EXE ON req.LCR_REQUEST_ID = HIS_EXE.LCR_REQUEST_ID AND HIS_EXE.STATUS = 'Executed'
LEFT JOIN LCR_STATUS_HISTORY HIS_CONV ON req.LCR_REQUEST_ID = HIS_CONV.LCR_REQUEST_ID AND HIS_CONV.STATUS = 'Converted to amendment.'
LEFT JOIN LCR_STATUS_HISTORY HIS_DEAD ON req.LCR_REQUEST_ID = HIS_DEAD.LCR_REQUEST_ID AND HIS_DEAD.STATUS = 'Dead'
LEFT JOIN LCR_STATUS_HISTORY HIS_COMP ON req.LCR_REQUEST_ID = HIS_COMP.LCR_REQUEST_ID AND HIS_COMP.STATUS = 'Completed'
LEFT JOIN LCR_STATUS_HISTORY HIS_EXP ON req.LCR_REQUEST_ID = HIS_EXP.LCR_REQUEST_ID AND HIS_EXP.STATUS = 'Expired/Terminated'
WHERE REQ.CATEGORY_NAME = 'Sales'
AND
REQ.DATE_RECORDED BETWEEN to_date ('07/01/2012', 'mm/dd/yyyy') AND to_date ('06/30/2013','mm/dd/yyyy')
GROUP BY
req.LCR_REQUEST_ID
,req.MATTER_ID
,req.CUSTOMER_NAME
,req.STATUS
,HIS.RECORDED_DATE
--,HIS.STATUS
,HIS_SUB.RECORDED_DATE
--,HIS_NA.RECORDED_DATE
,HIS_AtA.RECORDED_DATE
,HIS_SCIN.RECORDED_DATE
,HIS_EXE.RECORDED_DATE
,HIS_CONV.RECORDED_DATE
,HIS_DEAD.RECORDED_DATE
,HIS_COMP.RECORDED_DATE
,HIS_EXP.RECORDED_DATE
,CASE
WHEN req.DEALSIZE = 0 THEN ''
WHEN req.DEALSIZE = 1 THEN 'Less Than 100K'
when req.DEALSIZE = 2 THEN '100K-500K'
when req.DEALSIZE = 3 THEN '500K-1M'
when req.DEALSIZE = 4 THEN '1M-10M'
when req.DEALSIZE = 5 THEN '>10M'
END
,cm_out.PTY_NAME
,cm_out.PTY_ID
,req.customer_id
,cm_out.EFFECTIVE_DATE
,cm_out.INITIALTERM_END_DATE
,cm_out.CONTRACT_TERMINATION_DATE
,cm_out.TERM_NOTICE_NONCOMP_REASON
,cm_out.TERM_TYPE
,cm_out.INITIALTERM_DURATION
,cm_out.INVOICE_TIMEMEAS
,cm_out.NUMBER_OF_RENEWALS
,req.AMENDMENT
,prod.rpg
,prod.SALES_PRODUCT_NAME
,prod.CONTRACT_TYPE
,req.CONTRACT_ROLE
,cm_out.METADATA_FLAG
,CASE
WHEN cm_out.DATA_AUTO_IMPORT_YN = 1 THEN 'YES'
WHEN cm_out.REQUESTOR = 'Imported Record' THEN 'YES'
ELSE 'NO'
END
,CASE act.actionstatus
WHEN 'Cancel Date Update' THEN 'Yes'
ELSE NULL
END
,req_emp.FIRST_NAME || ' ' || req_emp.LAST_NAME
,att_emp.FIRST_NAME || ' ' || att_emp.LAST_NAME
,req.REQUESTOR_DEPARTMENT
,req.AGREEMENT_TITLE
,req.DATE_RECORDED;
I wonder how you get multiple columns with a max date using a single TOP 1?
Most of the LEFT JOINs to LCR_STATUS_HISTORY can easily be replaced by a single join using MAX(CASE):
LEFT JOIN
(
SELECT LCR_REQUEST_ID,
MAX(CASE WHEN STATUS = 'Assigned to Attorney' THEN RECORDED_DATE END) AS "AssignedToAttorney_Status_Date"
,MAX(CASE WHEN STATUS = 'Submission Complete-In Negotiation' THEN RECORDED_DATE END) AS "InNegotiation_Status_Date"
,...
FROM LCR_STATUS_HISTORY
GROUP BY LCR_REQUEST_ID
) his
ON HIS.LCR_REQUEST_ID = req.LCR_REQUEST_ID
Then you probably don't need the GROUP BY any more.
And in this join-condition the OR-part can be removed:
((HIS.STATUS = REQ.STATUS) OR (HIS.STATUS = 'Assigned to Attorney' AND REQ.STATUS = 'Assigned To Attorney'))
ON HIS.LCR_REQUEST_ID = req.LCR_REQUEST_ID