1

我有以下查询,但不是像“22-OCT-2012 AND 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
4

2 回答 2

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
于 2012-11-08T06:37:52.427 回答
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

希望这对你有帮助

于 2012-11-08T05:12:46.217 回答