2015-05-18 98 views
1

我需要一個查詢,它根據每月/每年返回數據。下面是一個子查詢我寫返回一行 - 起始日期日期和結束日期是我需要在我的主查詢使用WHERE子句中的條件(Oracle)

WITH SUBQ AS (SELECT 
    dim.MONTH_NAME as current_month_name 
    ,dim.year_period as current_month 
    ,dim.PERIOD_YEAR as YEAR 
    ,CASE WHEN dim.year_period NOT LIKE '%01' THEN to_number(CONCAT(to_char(dim.PERIOD_YEAR-1) , '01')) 
     WHEN dim.year_period LIKE '%01'THEN to_number(CONCAT(to_char(dim.PERIOD_YEAR-2) , '01')) 
     END AS START_DATE 
    ,CASE WHEN dim.year_period NOT LIKE '%01' THEN to_number(CONCAT(to_char(dim.PERIOD_YEAR) , '01' )) 
     WHEN dim.year_period LIKE '%01'THEN to_number(CONCAT(to_char(dim.PERIOD_YEAR-1) , '01' )) END AS ENDDATE 
    from dim_periods dim WHERE dim.year_period=to_number(to_char(sysdate, 'YYYYMM'))) 

問題是價值觀 - 如何在where子句中使用值從子查詢與一列? 我需要得到這樣的事情,我只是不明白我應該如何加入我的子查詢和我使用的表的其餘部分 -

select * from financial_data fd 
where fd.year_period BETWEEN subq.start_date and subq.enddate 

回答

0

你可以加入子查詢,即CTE與表,然後使用過濾器謂詞中的列名稱。 WITH子句中子查詢的結果就像臨時表

例如,

WITH SUBQ AS 
    (SELECT dim.MONTH_NAME AS current_month_name , 
    dim.year_period  AS current_month , 
    dim.PERIOD_YEAR  AS YEAR , 
    CASE 
     WHEN dim.year_period NOT LIKE '%01' 
     THEN to_number(CONCAT(TO_CHAR(dim.PERIOD_YEAR-1) , '01')) 
     WHEN dim.year_period LIKE '%01' 
     THEN to_number(CONCAT(TO_CHAR(dim.PERIOD_YEAR-2) , '01')) 
    END AS START_DATE , 
    CASE 
     WHEN dim.year_period NOT LIKE '%01' 
     THEN to_number(CONCAT(TO_CHAR(dim.PERIOD_YEAR) , '01')) 
     WHEN dim.year_period LIKE '%01' 
     THEN to_number(CONCAT(TO_CHAR(dim.PERIOD_YEAR-1) , '01')) 
    END AS ENDDATE 
    FROM dim_periods dim 
    WHERE dim.year_period=to_number(TO_CHAR(SYSDATE, 'YYYYMM')) 
) 
SELECT fd.COLUMNS, 
    q.COLUMNS 
FROM financial_data fd 
JOIN subq q 
ON (fd.KEY = q.KEY) -- join key 
WHERE fd.year_period BETWEEN q.start_date AND q.enddate; 

所以,SUBQ就像一個臨時表中,你financial_data表連接。

UPDATE OP不希望ANSI加入語法

WITH SUBQ AS 
    (SELECT dim.MONTH_NAME AS current_month_name , 
    dim.year_period  AS current_month , 
    dim.PERIOD_YEAR  AS YEAR , 
    CASE 
     WHEN dim.year_period NOT LIKE '%01' 
     THEN to_number(CONCAT(TO_CHAR(dim.PERIOD_YEAR-1) , '01')) 
     WHEN dim.year_period LIKE '%01' 
     THEN to_number(CONCAT(TO_CHAR(dim.PERIOD_YEAR-2) , '01')) 
    END AS START_DATE , 
    CASE 
     WHEN dim.year_period NOT LIKE '%01' 
     THEN to_number(CONCAT(TO_CHAR(dim.PERIOD_YEAR) , '01')) 
     WHEN dim.year_period LIKE '%01' 
     THEN to_number(CONCAT(TO_CHAR(dim.PERIOD_YEAR-1) , '01')) 
    END AS ENDDATE 
    FROM dim_periods dim 
    WHERE dim.year_period=to_number(TO_CHAR(SYSDATE, 'YYYYMM')) 
) 
SELECT fd.COLUMNS, 
    q.COLUMNS 
FROM financial_data fd, 
    subq q 
WHERE fd.KEY = q.KEY -- join key 
AND fd.year_period BETWEEN q.start_date AND q.enddate; 
+0

好的,但我如何做到這一點與WHERE子句中的連接? 因爲如果我儘量做到以下幾點: fd.year_period = subq2.current_month 和subq2.start_date和subq2.enddate之間fd.year_period我沒有得到任何行回來後,我只是不明白爲什麼 – user3014914

+0

@ user3014914我只要你ANSI語法。請參閱我的更新,瞭解Oracle等效語法。 –