SQL DB2 如何将日期变量应用于内部查询?

SQL DB2 how to apply date variable to inner queries?

我有已应用于外部查询的日期变量,但我收到一条错误消息:v_Date 在使用它的上下文中无效 - 当试图在内部查询中使用它时查询。

你能帮我用时间变量 v_Date 替换内部查询中的两个 3/31/2016 日期吗?我需要移动 WITH 行还是进行双连接?

WITH ttt as (select '3/31/2016' v_Date FROM SYSIBM.SYSDUMMY1)

SELECT
fpr.ID
fpr.ID_PRICG_MTHDY,
fpr.MARKET_DATE,
fpr.PRICE_AMOUNT AS FIRST_PRICE,
pr.PRICE_AMOUNT AS SECOND_PRICE,
pr2.PRICE_AMOUNT AS THIRD_PRICE,
thr.PRICE_DIFF AS TEST_1,
thr.PRICE_DIFF2 AS TEST_2
FROM PRICE_TABLE_1 fpr

   LEFT JOIN PRICE_TABLE0 pr
   ON fpr.ID = pr.ID AND pr.MARKET_DATE = '3/31/2016' AND pr.ROLE_TYPE = 'D'
   LEFT JOIN PRICE_TABLE0 pr2
   ON fpr.ID = pr2.ID and pr2.MARKET_DATE = '3/31/2016' AND pr2.ROLE_TYPE = 'P'
   LEFT JOIN THRESHOLD_TEST_TABLE thr
   ON fpr.ID_PRICG_MTHDY = thr.ID_PRICG_MTHDY

JOIN ttt ON fpr.MARKET_DATE = v_Date

WHERE fpr.DT_EXPTN = '1/1/9999'
   AND fpr.MARKET_DATE = fpr.SELL_DATE
   AND fpr.TYPE = 'F'
   AND fpr.ID_PRICG_MTHDY IN (1, 3, 4, 7)

我将从 CTE 作为 FROM 的第一个元素开始,然后您可以在任何地方使用该值:

FROM ttt LEFT JOIN
     PRICE_TABLE_1 fpr 
     ON fpr.MARKET_DATE = v_Date LEFT JOIN
     PRICE_TABLE0 pr
     ON fpr.ID = pr.ID AND pr.MARKET_DATE = ttt.v_date AND pr.ROLE_TYPE = 'D' LEFT JOIN
     PRICE_TABLE0 pr2
     ON fpr.ID = pr2.ID and pr2.MARKET_DATE = ttt.v_date AND pr2.ROLE_TYPE = 'P' LEFT JOIN
     THRESHOLD_TEST_TABLE thr
     ON fpr.ID_PRICG_MTHDY = thr.ID_PRICG_MTHDY