Oracle 根據日期範圍和班期統計數量

SELECT CASE WHEN bqcnt.diffDays <= 7 AND bqcnt.bq1 > bqcnt.bq2 THEN LENGTH(REPLACE(TRANSLATE(SUBSTR('1234567', bqcnt.bq1) || SUBSTR('1234567', DECODE(SIGN(bqcnt.bq2 - bqcnt.bq1), 1, bqcnt.bq1, 0), bqcnt.bq2), TRANSLATE(SUBSTR('1234567', bqcnt.bq1) || SUBSTR('1234567', DECODE(SIGN(bqcnt.bq2 - bqcnt.bq1), 1, bqcnt.bq1, 0), bqcnt.bq2), bqcnt.weeks, ' '), ' '), ' ', '') ) WHEN bqcnt.diffDays <= 7 AND bqcnt.bq1 < bqcnt.bq2 THEN LENGTH(REPLACE(TRANSLATE(SUBSTR('1234567', DECODE(SIGN(bqcnt.bq2 - bqcnt.bq1), 1, bqcnt.bq1, 0), bqcnt.bq2), TRANSLATE(SUBSTR('1234567', DECODE(SIGN(bqcnt.bq2 - bqcnt.bq1), 1, bqcnt.bq1, 0), bqcnt.bq2), bqcnt.weeks, ' '), ' '), ' ', '') ) WHEN bqcnt.diffDays <= 7 AND bqcnt.bq1 < bqcnt.bq2 THEN LENGTH(bqcnt.bq2) ELSE LENGTH(REPLACE(TRANSLATE(SUBSTR('1234567', bqcnt.bq1) || SUBSTR('1234567', 0, bqcnt.bq2), TRANSLATE(SUBSTR('1234567', bqcnt.bq1) || SUBSTR('1234567', 0, bqcnt.bq2), bqcnt.weeks, ' '), ' '), ' ', '') ) + CEIL(bqcnt.diffdays / 7 -1 ) * LENGTH(bqcnt.weeks) END cnt FROM ( SELECT TO_DATE(bqt.endDate, 'YYYY-MM-DD')- TO_DATE(bqt.beginDate, 'YYYY-MM-DD') diffDays, DECODE(TO_CHAR(TO_DATE(bqt.beginDate, 'YYYY-MM-DD'), 'D')-1, 0, 7, TO_CHAR(TO_DATE(bqt.beginDate, 'YYYY-MM-DD'), 'D')-1) bq1, DECODE(TO_CHAR(TO_DATE(bqt.endDate, 'YYYY-MM-DD'), 'D')-1, 0, 7, TO_CHAR(TO_DATE(bqt.endDate, 'YYYY-MM-DD'), 'D')-1) bq2, weeks FROM ( SELECT '2019-05-12' beginDate, '2019-05-26' endDate, '247' weeks from dual) bqt) bqcnt
相關文章
相關標籤/搜索