SQL中SubQuery中的子查询 - DB2

时间:2013-07-24 16:29:41

标签: sql db2 subquery

尝试根据查询返回的结果之一动态生成第一个过滤器中显示的子查询时遇到问题。有人可以告诉我我做错了什么。在第一个子查询中,它起作用了。

        ( 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

2 个答案:

答案 0 :(得分:3)

我想说这里的一个好选择是使用CTE或Common Table Expression。您可以执行类似以下操作:

WITH CTE_X AS(
SELECT VAL_A
      ,VAL_B
  FROM TABLE_A)
,CTE_Y AS(
SELECT VAL_C
      ,VAL_B
  FROM TABLE_B)
SELECT VAL_A
      ,VAL_B
  FROM CTE_X X
    JOIN CTE_Y Y
      ON X.VAL_A = Y.VAL_C;

虽然这不是特定于您的示例,但它确实显示CTE创建了一种临时的“内存”表,您可以在后续查询中访问该表。这应该允许您将内部两个子选择作为CTE发出,然后在“SELECT MAX(MAX_DATE - MIN_DATE)AS NUM_CONS_DAYS”查询中使用CTE。

答案 1 :(得分:-1)

您无法在子选择中引用外部选择中的列,无论如何深度不超过1级。如果我正确理解您正在做的事情,您可能需要在外部选择中加入DWH.MQT_SUMMARY_WATER_READINGSDWH.DWH_WMETER_CONTEXT