获取具有系统日期和状态的行

时间:2012-05-10 10:51:01

标签: sql database oracle

>          cycleNO| month |   year|  start date |    close date         |state | prv month  | next mnt 
>             1      4        2012    1/4/2012     30/4/2012 23:59:59      1       3                5
>             1      5        2012    1/5/2012     31/5/2012 23:59:59      0       4                6  
>             1      6        2012    1/6/2012     30/6/2012 23:59:59      0       5                7  
>             1      7        2012    1/7/2012     31/7/2012 23:59:59      0       6                8
>             2      4        2012    1/4/2012     30/4/2012 23:59:59      1       3                5
>             2      5        2012    1/5/2012     31/5/2012 23:59:59      0       4                6  
>             2      6        2012    1/6/2012     30/6/2012 23:59:59      0       5                7  
>             2      7        2012    1/7/2012     31/7/2012 23:59:59      0       6                8

我有一个表(cycle_set),如上所述,想要获取(cycleNO,月,年,开始日期,结束日期)条件,其中state = 0和start-close日期,其中包含系统日期也是立即下个月,州= 0。

输出应为:

cycleNO | month | year | start date | close date 
1      5        2012    1/5/2012     31/5/2012 23:59:59
1      6        2012    1/6/2012     30/6/2012 23:59:59
2      5        2012    1/5/2012     31/5/2012 23:59:59
2      6        2012    1/6/2012     30/6/2012 23:59:59

3 个答案:

答案 0 :(得分:1)

SELECT cycleNO,month,year,start_date,close_date FROM cycle_set
WHERE state=0
AND MONTH(start_date) = (SELECT month FROM cycle_set WHERE state=0 AND...)
AND MONTH(close_date) = (SELECT month FROM cycle_set WHERE state=0 AND...)

问题是,您无法选择下一个结果月份。这两个子选择中WHERE子句的内容是什么?

如果您提取所有数据,然后使用PHP或其他任何数据进行处理,那将会容易得多。

答案 1 :(得分:1)

select cycleNO,month,year,start_date,close_date 
FROM cycle_set
WHERE state=0 and sysdate between start_date and close_date

更新:如果您想同时获得当前和下个月:

select cycleNO, month, year, start_date, close_date 
FROM cycle_set
WHERE state=0 
   and ( sysdate between start_date and close_date or --current month
         sysdate between add_months(start_date,-1)  and close_date --next_month
        )

答案 2 :(得分:0)

假设您有一个数据集(如示例所示):

    SELECT  cycleno,month,year,start_date,close_date 
      FROM  cycle_set
     WHERE  state=0 
       AND  (   month = EXTRACT(month FROM sysdate) 
             OR month = EXTRACT(month FROM sysdate)+1
            )
  ORDER BY  cycleno, month, year, start_date