周的范围而不是放置日期

时间:2012-11-08 02:51:15

标签: oracle

我有以下查询,但不是像'22 -OCT-2012和28-OCT-2012那样放置一周的范围'我会放置像CurrentWeek -2或CurrentWeek-1这样的代码,这将避免编辑每周我需要运行它的查询。 你知道怎么做吗?

感谢 LD

    SELECT WO.USER_6 AS STYLE
 ,SUM (CASE WHEN (OPERATION.STATUS ='C' AND OPERATION.CLOSE_DATE BETWEEN '22-OCT-2012' AND '28-OCT-2012') THEN OPERATION.RUN_HRS ELSE 0 END) WEEK43
 ,SUM (CASE WHEN (OPERATION.STATUS ='C' AND OPERATION.CLOSE_DATE BETWEEN '29-OCT-2012' AND '04-NOV-2012') THEN OPERATION.RUN_HRS ELSE 0 END) WEEK44
FROM WORK_ORDER WO, OPERATION
WHERE  WO.BASE_ID = OPERATION.WORKORDER_BASE_ID
AND WO.Lot_ID = Operation.Workorder_Lot_ID
        AND WO.Sub_ID = Operation.Workorder_Sub_ID
        AND WO.Split_ID = Operation.Workorder_Split_ID
AND WO.TYPE ='W'
AND WO.WAREHOUSE_ID ='MEX-04'
AND OPERATION.CLOSE_DATE BETWEEN '22-OCT-2012'  AND '04-NOV-2012'
AND OPERATION.RESOURCE_ID IN ('171-4','171-ADD','171-3' ,'BAMEX-SEWCONC','BAMEX-SEWPATC')
AND OPERATION.RUN > 0
GROUP BY
WO.USER_6

2 个答案:

答案 0 :(得分:0)

如果我理解了您的问题,那么您可以使用它来通过当前的工作日

SUM (CASE WHEN (OPERATION.STATUS ='C' AND OPERATION.CLOSE_DATE BETWEEN to_char(trunc(sysdate),'DD-MON-YYYY')  and to_char(trunc(sysdate)-6,'DD-MON-YYYY')) THEN OPERATION.RUN_HRS ELSE 0 END) WEEK43

希望这有帮助你

答案 1 :(得分:0)

在这种情况下,我将使用trunc函数:

currentweek will be trunc(sysdate,'D') 
current_week - 1 will be trunc(sysdate,'D') - 7
current week - 2 will be trunc(sysdate,'D') - 2 * 7

注意这将是星期几的第一天。如果你想要星期一你应该和有一天:

current week - 2 will be trunc(sysdate,'D') - 2 * 7 + 1

<强>更新 弗兰克是对的,一周中第一天的行为取决于NLS_TERITORY

alter session set NLS_TERRITORY ='UNITED KINGDOM';
select trunc(sysdate,'D') from dual;
05-11-2012

alter session set NLS_TERRITORY ='AMERICA';
select trunc(sysdate,'D') from dual;
04-11-2012