如何在oracle中声明类似Start_date,end_date的日期参数?以及如何传递它?

时间:2018-02-14 05:27:28

标签: oracle oracle-sqldeveloper

我的查询是

select  
    '1' as KPI_ID, 'P2P' as KPI_DESC,'22-MAR-17' as dates,
    (sum(case when S_DATE between add_months('22-MAR-17',0)-13
                          and add_months('22-MAR-17',0)-7  then VOLUME else 0 end )) LAST_WEEK_Volume,
    (sum(case when S_DATE between add_months('22-MAR-17',0)-6
                          and add_months('22-MAR-17',0)  then VOLUME else 0 end )) THIS_WEEK_Volume
from 
    table

问题陈述 - 实际上我有两年的数据..我想要计算上周的量,当前一周的量明天为2年。那么我应该在查询中修改什么。当前查询只给出1天的计算。我想要每天做明智的计算。根据谷歌我发现,在定义参数start_date和end_date并传递到列之后它是可能的,你可以帮我解决一下吗?

2 个答案:

答案 0 :(得分:2)

RTFM?

Oracle内置函数TO_DATE就是您所需要的。

请参阅Oracle documentation

select TO_DATE(`14-FEB-2018`,`DD-MON-YYYY`) from DUAL

更新

回复你的评论......

  

其实我是pl-sql的新手

我建议(再次)RTFM。

转到Oracle数据库Documentation网页并选择适当的Oracle数据库版本。

关于PL / SQL,我建议PL/SQL Language Reference

答案 1 :(得分:1)

您可以使用替换

        define my_date = '''22-MAR-17''';
         select  
           '1' as KPI_ID, 'P2P' as KPI_DESC, TO_DATE(&my_date,'DD-MON-YY')  as dates,
            (sum(case when S_DATE between add_months(TO_DATE(&my_date,'DD-MON-YY'),0)-13
            and add_months(TO_DATE(&my_date,'DD-MON-YY'),0)-7  then VOLUME else 0 end )) LAST_WEEK_Volume,
            (sum(case when S_DATE between add_months(TO_DATE(&my_date,'DD-MON-YY') ,0)-6
            and add_months(TO_DATE(&my_date,'DD-MON-YY') ,0)  then VOLUME else 0 end )) THIS_WEEK_Volume
            from 
            table;
         undefine my_date;