嵌套用于具有递归子查询的循环

时间:2016-02-03 09:00:28

标签: plsql recursive-query

如何运行以下语句,为不同表格中的每个员工返回一行或多行,并在一周或其他范围内返回每个日期?

DECLARE
    CURSOR emps_in IS
    SELECT EMPLOYEE_ID FROM EMPLOYEES;

    CURSOR days_in IS
    SELECT TO_CHAR(DAYS, 'YYYYMMDD') FROM WEEKS WHERE WEEK_NUM=1;

    BEGIN
        FOR e IN emps_in LOOP
            FOR d IN days_in LOOP
                INSERT INTO acc_out_time (ROW_NUM, ENTRY_ID, ENTRY_DATE, ENTRY_TIME, TIME, PIN, DEVICE_ID, STATE, EVENT_POINT_NAME, DUPLICATE)
                WITH BDEV_RPT2 AS
                (SELECT *
                    FROM (SELECT ROW_NUMBER()OVER(ORDER BY TIME DESC) ROW_NUM, a.* FROM acc_time_logs a WHERE PIN=e AND ENTRY_DATE=d AND STATE IN ('1','2') AND DEVICE_ID IN ('18','10','6','9') ORDER BY TIME DESC)
                    WHERE ROW_NUM IN ('1','2') AND DEVICE_ID='18' OR (ROW_NUM='1' AND DEVICE_ID IN ('10','6','9') AND NOT EXISTS(SELECT NULL FROM (SELECT ROW_NUMBER()OVER(ORDER BY TIME DESC) ROW_NUM, a.* FROM acc_time_logs a WHERE PIN=e AND ENTRY_DATE=d AND STATE IN ('1','2') AND DEVICE_ID IN ('18','10','6','9') ORDER BY TIME DESC) WHERE ROW_NUM IN ('1','2') AND DEVICE_ID='18')))
                SELECT ROW_NUMBER()OVER(ORDER BY TIME ASC) ROW_NUM, ENTRY_ID, ENTRY_DATE, ENTRY_TIME, TIME, PIN, DEVICE_ID, STATE, EVENT_POINT_NAME, DUPLICATE FROM BDEV_RPT2;
            END LOOP;
        END LOOP;
    COMMIT;
END;

0 个答案:

没有答案