我在嘗試根據查詢返回的結果之一動態地在第一個篩選器中顯示子查詢時遇到問題。有人可以告訴我我做錯了什麼。在第一個子查詢中工作。SQL中的子查詢中的子查詢 - DB2
(SELECT
MAX(MAX_DATE - MIN_DATE) AS NUM_CONS_DAYS
FROM
(
SELECT
MIN(TMP.D_DAT_INDEX_DATE) AS MIN_DATE,
MAX(TMP.D_DAT_INDEX_DATE) AS MAX_DATE,
SUM(INDEX_COUNT) AS SUM_INDEX
FROM
(
SELECT
D_DAT_INDEX_DATE,
INDEX_COUNT,
D_DAT_INDEX_DATE - (DENSE_RANK() OVER(ORDER BY D_DAT_INDEX_DATE)) DAYS AS G
FROM
DWH.MQT_SUMMARY_WATER_READINGS
WHERE
N_COD_METER_CNTX_KEY = 79094
) AS TMP
GROUP BY
TMP.G
ORDER BY
1
)) AS MAX_NUM_CONS_DAYS
以上是我試圖從查詢與CTXTKEY或CTXT.N_COD_METER_CNTX_KEY替換123456的子查詢。以下是完整的代碼。請注意,比在「MAX_NUM_CONS_DAYS」之前的子查詢中有效。但是,這只是一個子查詢。
SELECT
N_COD_WM_DWH_KEY,
V_COD_WM_SN_2,
N_COD_SP_ID,
CTXKEY,
V_COD_MIU_SN,
N_COD_POD,
MIU_CAT,
V_COD_SITR_ASSOCIATED,
WO_INST_DATE,
WO_MIU_CAT,
DAYSRECEIVED3,
MAX_NUM_CONS_DAYS,
(CASE WHEN (DAYSRECEIVED3 = 3) THEN 'Y' ELSE 'N' END) AS GREEN,
(CASE WHEN (DAYSRECEIVED3 < 3 AND DAYSRECEIVED3 > 0) THEN 'Y' ELSE 'N' END) AS BLUE,
(CASE WHEN (DAYSRECEIVED3 = 0 AND MAX_NUM_CONS_DAYS >= 5) THEN 'Y' ELSE 'N' END) AS ORANGE,
(CASE WHEN (DAYSRECEIVED3 = 0 AND MAX_NUM_CONS_DAYS BETWEEN 1 and 4) THEN 'Y' ELSE 'N' END) AS RED
FROM
(
SELECT
WMETER.N_COD_WM_DWH_KEY,
WMETER.V_COD_WM_SN_2,
WMETER.N_COD_SP_ID,
CTXT.N_COD_METER_CNTX_KEY AS CTXKEY,
CTXT.V_COD_MIU_SN,
CTXT.N_COD_POD,
MIU.N_COD_MIU_CATEGORY AS MIU_CAT,
CTXT.V_COD_SITR_ASSOCIATED,
T1.D_DAT_PLAN_INST AS WO_INST_DATE,
T1.N_COD_MIU_CATEGORY AS WO_MIU_CAT,
(SELECT COUNT(DISTINCT D_DAT_INDEX_DATE) FROM DWH.MQT_SUMMARY_WATER_READINGS WHERE (N_COD_METER_CNTX_KEY = CTXT.N_COD_METER_CNTX_KEY) AND D_DAT_INDEX_DATE BETWEEN ('2013-07-10') AND ('2013-07-12')) AS DAYSRECEIVED3,
(SELECT
MAX(MAX_DATE - MIN_DATE) AS NUM_CONS_DAYS
FROM
(
SELECT
MIN(TMP.D_DAT_INDEX_DATE) AS MIN_DATE,
MAX(TMP.D_DAT_INDEX_DATE) AS MAX_DATE,
SUM(INDEX_COUNT) AS SUM_INDEX
FROM
(
SELECT
D_DAT_INDEX_DATE,
INDEX_COUNT,
D_DAT_INDEX_DATE - (DENSE_RANK() OVER(ORDER BY D_DAT_INDEX_DATE)) DAYS AS G
FROM
DWH.MQT_SUMMARY_WATER_READINGS
WHERE
N_COD_METER_CNTX_KEY = 79094
) AS TMP
GROUP BY
TMP.G
ORDER BY
1
)) AS MAX_NUM_CONS_DAYS
FROM DWH.DWH_WATER_METER AS WMETER
LEFT JOIN DWH.DWH_WMETER_CONTEXT AS CTXT
ON WMETER.N_COD_WM_DWH_KEY = CTXT.N_COD_WM_DWH_KEY
LEFT JOIN DWH.DWH_MIU AS MIU
ON CTXT.V_COD_MIU_SN = MIU.V_COD_MIU_SN
LEFT JOIN
(SELECT V_COD_CORR_WAT_METER_SN, D_DAT_PLAN_INST, N_COD_MIU_CATEGORY
FROM DWH.DWH_ORDER_MANAGEMENT_FACT
JOIN DWH.DWH_MIU
ON DWH.DWH_ORDER_MANAGEMENT_FACT.V_COD_MIU_SN = DWH.DWH_MIU.V_COD_MIU_SN
) AS T1
ON WMETER.V_COD_WM_SN_2 = T1.V_COD_CORR_WAT_METER_SN
WHERE
(V_COD_SITR_ASSOCIATED = 'X')
AND ((MIU.N_COD_MIU_CATEGORY <> 4) OR ((MIU.N_COD_MIU_CATEGORY IS NULL) AND ((T1.N_COD_MIU_CATEGORY <> 4 ) OR (T1.N_COD_MIU_CATEGORY IS NULL ))))
)
錯誤,我得到的是:
錯誤代碼:-204,SQL狀態:42704
Thanks mustaccio 我沒有意識到在子查詢中引用有任何限制。 – maldman