根据另一个表中colomn的结果更改查询日期

时间:2016-02-25 07:39:07

标签: sql plsql oracle11g

我有一个像这样的SQL查询:

WITH CUR_DATE AS(
 SELECT to_date(cur_value, 'DD/MM/YYYY') AS cur_value
 FROM PARAMS
 WHERE id = 'CURRENT_SERVER_DATE'
)
SELECT 
   cur_date.cur_value - 1 as date_cal,
   iar.allocation_date
FROM POS
 CROSS JOIN CUR_DATE
 INNER JOIN REFERENCE
   ON RC.reference_id = REFERENCE.id

我希望修改' date_cal'根据' GENERATION_TYPE'的结果来自另一个查询:

select distinct(GENERATION_TYPE) 
 from VVV_PICKING_HIST where CREATION_DATE =    (select MAX(CREATION_DATE)
 from VVV_PICKING_HIST

表示如果GENERATION_TYPE =" NO"我留下cur_date.cur_value - 1否则我删除了-1而且我会有

cur_date.cur_value as date_cal

我不知道如何合并两个查询并使用例如

CASE WHEN GENERATION_TYPE = "NO" 
THEN cur_date.cur_value - 1 AS DATE_CAL  ELSE cur_date.cur_value AS DATE_CAL

1 个答案:

答案 0 :(得分:0)

试试这个:

WITH CUR_DATE AS(
 SELECT to_date(cur_value, 'DD/MM/YYYY') AS cur_value
 FROM PARAMS
 WHERE id = 'CURRENT_SERVER_DATE'
)
SELECT 
   CASE WHEN GENERATION_TYPE = "NO" 
        THEN cur_date.cur_value - 1 
        ELSE cur_date.cur_value end as date_cal,
   iar.allocation_date
FROM POS
 CROSS JOIN CUR_DATE
 INNER JOIN REFERENCE
   ON RC.reference_id = REFERENCE.id
 INNER JOIN(select distinct(GENERATION_TYPE) as GENERATION_TYPE
            from VVV_PICKING_HIST where CREATION_DATE = (select MAX(CREATION_DATE)
                                                         from VVV_PICKING_HIST))
   ON(1=1)