WITH SubQry AS
(SELECT DISTINCT
TBL1."IDNUM"
FROM
"LIBRARY"."SALES" TBL4 INNER JOIN "LIBRARY"."CUSTOMER" TBL1 ON TBL4."CUSTOMERKEY" = TBL1."CUSTOMERKEY"
INNER JOIN "LIBRARY"."TIME" TBL3 ON TBL4."DATEKEY" = TBL3."DATEKEY"
INNER JOIN "LIBRARY"."PRODUCT" TBL2 ON TBL4."PRODUCTKEY" = TBL2."PRODUCTKEY"
WHERE
TBL1."COUNTRY" IN ('Austria', 'Denmark', 'Belgium')
AND TBL3."FISCALYEAR" BETWEEN '2011' AND '2016')
SELECT
T1."REGION"
, T1."COUNTRY"
, SubQry."IDNUM" AS "BOOKING_IDNUM"
, (CASE WHEN SubQry."IDNUM" IS NULL Then 'New' ELSE 'Existing' END) "BOOKING_FLAG"
, T1."IDNUM"
, T1."CUSTOMER"
, T1. "BUSSINESS"
, T1."PROJECTNUM"
, T1."PROJECTNAME"
, T1."PROJECTOWNER"
, T1."PROJECT_STATUS"
, SUM(T1."PROJECT_VALUE")
, COUNT(*) "TRANSACTION_COUNT"
, TO_CHAR(trunc(T1."CREATEDATE" / 100)) AS "YEARMONTH_CALENDAR"
, (CASE
WHEN T1."BUSS_ID" IN ('ABC', 'DEF')
THEN 'MID' ELSE NULL END) "DRIVE_TYPE"
, (CASE
WHEN T1."IDNUM" IN ('901023','1401516','2401056','2101444')
THEN 'VIP' ELSE 'Standard' END) AS "ACC_TYPE"
, T1."BUSS_ID"
FROM
"LIBRARY"."PROJECT_DETAILS" T1 LEFT JOIN SubQry ON T1."IDNUM" = SubQry."IDNUM"
WHERE
T1."COUNTRY" IN ('Austria', 'Denmark', 'Belgium')
AND T1."BUSS_ID" IN ('ABC', 'DEF')
AND T1."CREATEDATE" >= '20160101'
GROUP BY
T1."REGION"
, T1."COUNTRY"
, SubQry."IDNUM"
, T1."IDNUM"
, T1."CUSTOMER"
, T1. "BUSSINESS"
, T1."PROJECTNUM"
, T1."PROJECTNAME"
, T1."PROJECTOWNER"
, T1."PROJECT_STATUS"
, T1."BUSS_ID"
, T1."CREATEDATE"
HAVING
SUM(T1."PROJECT_VALUE") > 0
UNION
SELECT
T1."REGION"
, T1."COUNTRY"
, SubQry."IDNUM" AS "BOOKING_IDNUM"
, (CASE WHEN SubQry."IDNUM" IS NULL Then 'New' ELSE 'Existing' END) "BOOKING_FLAG"
, T1."IDNUM"
, T1."CUSTOMER"
, T1."BUSSINESS"
, T1."PROJECTNUM"
, T1."PROJECTNAME"
, T1."PROJECTOWNER"
, T1."PROJECT_STATUS"
, SUM(T1."PROJECT_VALUE")
, COUNT(*) "TRANSACTION_COUNT"
, TO_CHAR(trunc(T1."CREATEDATE" / 100)) AS "CREATEDATE_YEARMONTH_CALENDAR"
, (CASE
WHEN T1."BUSS_ID" IN ('ABC', 'JKL', 'PQR')
THEN 'PRO'ELSE NULL END) "DRIVE_TYPE"
, (CASE
WHEN T1."IDNUM" IN ('901023','1401516','2401056','2101444')
THEN 'VIP' ELSE 'Standard' END) AS "ACC_TYPE"
, T1."BUSS_ID"
FROM
"LIBRARY"."PROJECT_DETAILS" T1 LEFT JOIN SubQry ON T1."IDNUM" = SubQry."IDNUM"
WHERE
T1."COUNTRY" IN ('Austria', 'Denmark', 'Belgium')
AND T1."BUSS_ID" IN ('ABC', 'JKL', 'PQR')
AND T1."CREATEDATE" >= '20160101'
GROUP BY
T1."REGION"
, T1."COUNTRY"
, SubQry."IDNUM"
, T1."IDNUM"
, T1."CUSTOMER"
, T1."BUSSINESS"
, T1."PROJECTNUM"
, T1."PROJECTNAME"
, T1."PROJECTOWNER"
, T1."PROJECT_STATUS"
, T1."BUSS_ID"
, T1."CREATEDATE"
HAVING
SUM(T1."PROJECT_VALUE") > 0
您好,
以上查询包括:
"IDNUM"
。"IDNUM"
。此外,此查询应检查选定的"IDNUM"
是否与特定"BUSS_ID"
匹配,并在名为'MID'
的列中将其标记为"DRIVE_TYPE"
。"IDNUM"
的列中将'PRO'
标记为"DRIVE_TYPE"
。您可能已经注意到,在两个查询中都有一个共同的"BUSS_ID": 'ABC'
, (CASE WHEN T1."BUSS_ID" IN ('ABC', 'DEF') THEN 'MID' ELSE NULL END) "DRIVE_TYPE"
, (CASE WHEN T1."BUSS_ID" IN ('ABC', 'JKL', 'PQR') THEN 'PRO'ELSE NULL END) "DRIVE_TYPE"
我尝试过UNION这些查询来显示每一行,它们都是“BUSS_ID”,作为两行单独的结果。换句话说,如果一行有"BUSS_ID" of 'ABC'
,那么select语句会显示两次。一行"DRIVE_TYPE"
标记为'MID'
,另一行标记为'PRO'
。
问题是,当我运行查询时,我收到以下错误:
ORA-00604:递归SQL级别1发生错误
ORA-00904:“来自$ _subquery $ _003”。“DATEKEY_0_0”:无效的标识符
对我来说更令人困惑的是,我可以运行 WHERE CLAUSE + 1st Query 或 WITH CLAUSE + 2nd Query 的组合没有任何问题。 但是当我尝试运行 WITH CLAUSE + 1st Query + UNION + 2nd Query 时,我收到错误。
答案 0 :(得分:0)
这是关于CTE的伟大之处。试着用3个CTE重写一下,我敢打赌,如果你还有一个
,错误信息会更好WITH SubQry AS
(SELECT DISTINCT
TBL1."IDNUM"
FROM
"LIBRARY"."SALES" TBL4 INNER JOIN "LIBRARY"."CUSTOMER" TBL1 ON TBL4."CUSTOMERKEY" = TBL1."CUSTOMERKEY"
INNER JOIN "LIBRARY"."TIME" TBL3 ON TBL4."DATEKEY" = TBL3."DATEKEY"
INNER JOIN "LIBRARY"."PRODUCT" TBL2 ON TBL4."PRODUCTKEY" = TBL2."PRODUCTKEY"
WHERE
TBL1."COUNTRY" IN ('Austria', 'Denmark', 'Belgium')
AND TBL3."FISCALYEAR" BETWEEN '2011' AND '2016'), Q1 AS (
SELECT
T1."REGION"
, T1."COUNTRY"
, SubQry."IDNUM" AS "BOOKING_IDNUM"
, (CASE WHEN SubQry."IDNUM" IS NULL Then 'New' ELSE 'Existing' END) "BOOKING_FLAG"
, T1."IDNUM"
, T1."CUSTOMER"
, T1. "BUSSINESS"
, T1."PROJECTNUM"
, T1."PROJECTNAME"
, T1."PROJECTOWNER"
, T1."PROJECT_STATUS"
, SUM(T1."PROJECT_VALUE")
, COUNT(*) "TRANSACTION_COUNT"
, TO_CHAR(trunc(T1."CREATEDATE" / 100)) AS "YEARMONTH_CALENDAR"
, (CASE
WHEN T1."BUSS_ID" IN ('ABC', 'DEF')
THEN 'MID' ELSE NULL END) "DRIVE_TYPE"
, (CASE
WHEN T1."IDNUM" IN ('901023','1401516','2401056','2101444')
THEN 'VIP' ELSE 'Standard' END) AS "ACC_TYPE"
, T1."BUSS_ID"
FROM
"LIBRARY"."PROJECT_DETAILS" T1 LEFT JOIN SubQry ON T1."IDNUM" = SubQry."IDNUM"
WHERE
T1."COUNTRY" IN ('Austria', 'Denmark', 'Belgium')
AND T1."BUSS_ID" IN ('ABC', 'DEF')
AND T1."CREATEDATE" >= '20160101'
GROUP BY
T1."REGION"
, T1."COUNTRY"
, SubQry."IDNUM"
, T1."IDNUM"
, T1."CUSTOMER"
, T1. "BUSSINESS"
, T1."PROJECTNUM"
, T1."PROJECTNAME"
, T1."PROJECTOWNER"
, T1."PROJECT_STATUS"
, T1."BUSS_ID"
, T1."CREATEDATE"
HAVING
SUM(T1."PROJECT_VALUE") > 0
), Q2 AS
(
SELECT
T1."REGION"
, T1."COUNTRY"
, SubQry."IDNUM" AS "BOOKING_IDNUM"
, (CASE WHEN SubQry."IDNUM" IS NULL Then 'New' ELSE 'Existing' END) "BOOKING_FLAG"
, T1."IDNUM"
, T1."CUSTOMER"
, T1."BUSSINESS"
, T1."PROJECTNUM"
, T1."PROJECTNAME"
, T1."PROJECTOWNER"
, T1."PROJECT_STATUS"
, SUM(T1."PROJECT_VALUE")
, COUNT(*) "TRANSACTION_COUNT"
, TO_CHAR(trunc(T1."CREATEDATE" / 100)) AS "CREATEDATE_YEARMONTH_CALENDAR"
, (CASE
WHEN T1."BUSS_ID" IN ('ABC', 'JKL', 'PQR')
THEN 'PRO'ELSE NULL END) "DRIVE_TYPE"
, (CASE
WHEN T1."IDNUM" IN ('901023','1401516','2401056','2101444')
THEN 'VIP' ELSE 'Standard' END) AS "ACC_TYPE"
, T1."BUSS_ID"
FROM
"LIBRARY"."PROJECT_DETAILS" T1 LEFT JOIN SubQry ON T1."IDNUM" = SubQry."IDNUM"
WHERE
T1."COUNTRY" IN ('Austria', 'Denmark', 'Belgium')
AND T1."BUSS_ID" IN ('ABC', 'JKL', 'PQR')
AND T1."CREATEDATE" >= '20160101'
GROUP BY
T1."REGION"
, T1."COUNTRY"
, SubQry."IDNUM"
, T1."IDNUM"
, T1."CUSTOMER"
, T1."BUSSINESS"
, T1."PROJECTNUM"
, T1."PROJECTNAME"
, T1."PROJECTOWNER"
, T1."PROJECT_STATUS"
, T1."BUSS_ID"
, T1."CREATEDATE"
HAVING
SUM(T1."PROJECT_VALUE") > 0
)
SELECT * FROM Q1
UNION
SELECT * FROM Q2
另外考虑将第一个CTE更改为使用与IDNUM不同的名称,可能是BOOKING_IDNUM
WITH SubQry AS
(SELECT DISTINCT
TBL1.BOOKING_IDNUM
-- etc