使用Oracle SQL Developer执行此操作的最佳方法是什么?
我需要检查表Primary中的14天(两周mon_wk_1,tue_wk_2等等列)以查看是否在一周中的任何一天显示任何小时,如果有,则将当天的小时值插入到load_table中的相应字段。
如果主表中的任何日期都没有小时数,则检查辅助表中的14天列以查看是否有任何小时数,如果有,则使用辅助表的值插入load_table。如果主表和辅助表都没有显示这两周的任何小时数,那么请使用默认表中默认记录中的客户端值。
我正在考虑总结天数,然后如果sum> 0使用插入的主表值,如果某些< 0使用,则不确定如何设置是否需要使用默认值。有没有更好的方法来做到这一点
这样的事情:
BEGIN
IF
SELECT (SUM
(NVL(MON_HRS_1,0) +
NVL(TUE_HRS_1,0) +
NVL(WED_HRS_1,0) +
NVL(THUR_HRS_1,0) +
NVL(FRI_HRS_1,0) +
NVL(SAT_HRS_1,0) +
NVL(SUN_HRS_1,0) +
NVL(MON_HRS_2,0) +
NVL(TUE_HRS_2,0) +
NVL(WED_HRS_2,0) +
NVL(THUR_HRS_2,0) +
NVL(FRI_HRS_2,0) +
NVL(SAT_HRS_2,0) +
NVL(SUN_HRS_2,0)) AS TOTAL
FROM PRIMARY_TABLE PT
JOIN EMPLOYEE E
ON PT.CLIENT_ID = E.CLIENT_ID
AND PT.EE_ID = E.EE_ID
WHERE CLIENT_ID = 'FOOBAR') >0
THEN
INSERT INTO LOAD_TABLE
SELECT
E.EMPLOYEE_NAME,
E.EMPLOYEE_NUMBER,
E.EE_ID,
PT.NVL(MON_HRS_1,0),
PT.NVL(TUE_HRS_1,0),
PT.NVL(WED_HRS_1,0),
PT.NVL(THUR_HRS_1,0),
PT.NVL(FRI_HRS_1,0),
PT.NVL(SAT_HRS_1,0),
PT.NVL(SUN_HRS_1,0),
PT.NVL(MON_HRS_2,0),
PT.NVL(TUE_HRS_2,0),
PT.NVL(WED_HRS_2,0),
PT.NVL(THUR_HRS_2,0),
PT.NVL(FRI_HRS_2,0),
PT.NVL(SAT_HRS_2,0),
PT.NVL(SUN_HRS_2,0),
DATE(SYSDATE),
1 AS USER_ID
FROM PRIMARY_TABLE PT
JOIN EMPLOYEE E
ON PT.CLIENT_ID = E.CLIENT_ID
AND PT.EE_ID = E.EE_ID
WHERE CLIENT_ID = 'FOOBAR');
ELSE
INSERT INTO LOAD_TABLE
(
SELECT
E.EMPLOYEE_NAME,
E.EMPLOYEE_NUMBER,
E.EE_ID,
PT.NVL(MON_HRS_1,0),
PT.NVL(TUE_HRS_1,0),
PT.NVL(WED_HRS_1,0),
PT.NVL(THUR_HRS_1,0),
PT.NVL(FRI_HRS_1,0),
PT.NVL(SAT_HRS_1,0),
PT.NVL(SUN_HRS_1,0),
PT.NVL(MON_HRS_2,0),
PT.NVL(TUE_HRS_2,0),
PT.NVL(WED_HRS_2,0),
PT.NVL(THUR_HRS_2,0),
PT.NVL(FRI_HRS_2,0),
PT.NVL(SAT_HRS_2,0),
PT.NVL(SUN_HRS_2,0),
DATE(SYSDATE),
1 AS USER_ID
FROM SECONDAY_TABLE PT
JOIN EMPLOYEE E
ON PT.CLIENT_ID = E.CLIENT_ID
AND PT.EE_ID = E.EE_ID
WHERE CLIENT_ID = 'FOOBAR'
);
END IF;
END;
DATA SOURCE TABLES:
主要表
EE_ID VARCHAR2,
CLIENT_ID VARCHAR2,
MON_HRS_1 NUMBER,
TUE_HRS_1 NUMBER,
WED_HRS_1 NUMBER,
THUR_HRS_1 NUMBER,
FRI_HRS_1 NUMBER,
SAT_HRS_1 NUMBER,
SUN_HRS_1 NUMBER,
MON_HRS_2 NUMBER,
TUE_HRS_2 NUMBER,
WED_HRS_2 NUMBER,
THUR_HRS_2 NUMBER,
FRI_HRS_2 NUMBER,
SAT_HRS_2 NUMBER,
SUN_HRS_2 NUMBER,
ACTUAL_HRS_WK NUMBER,
COMMENTS VARCHAR2
SECONDARY TABLE
EE_ID VARCHAR2,
CLIENT_ID VARCHAR2,
SCHEDULE_ID VARCHAR2
MON_HRS_1 NUMBER,
TUE_HRS_1 NUMBER,
WED_HRS_1 NUMBER,
THUR_HRS_1 NUMBER,
FRI_HRS_1 NUMBER,
SAT_HRS_1 NUMBER,
SUN_HRS_1 NUMBER,
MON_HRS_2 NUMBER,
TUE_HRS_2 NUMBER,
WED_HRS_2 NUMBER,
THUR_HRS_2 NUMBER,
FRI_HRS_2 NUMBER,
SAT_HRS_2 NUMBER,
SUN_HRS_2 NUMBER,
ACTUAL_HRS_WK NUMBER,
COMMENTS VARCHAR2
Client_default
CLIENT_ID VARCHAR2,
MON_HRS_1 NUMBER,
TUE_HRS_1 NUMBER,
WED_HRS_1 NUMBER,
THUR_HRS_1 NUMBER,
FRI_HRS_1 NUMBER,
SAT_HRS_1 NUMBER,
SUN_HRS_1 NUMBER,
MON_HRS_2 NUMBER,
TUE_HRS_2 NUMBER,
WED_HRS_2 NUMBER,
THUR_HRS_2 NUMBER,
FRI_HRS_2 NUMBER,
SAT_HRS_2 NUMBER,
SUN_HRS_2 NUMBER,
ACTUAL_HRS_WK NUMBER
Load_table
EMPLOYEE_NAME VARCHAR2,
EMPLOYEE_NUMBER NUMBER,
EE_ID VARCHAR VARCHAR2,
MON_HRS_1 NUMBER,
TUE_HRS_1 NUMBER,
WED_HRS_1 NUMBER,
THUR_HRS_1 NUMBER,
FRI_HRS_1 NUMBER,
SAT_HRS_1 NUMBER,
SUN_HRS_1 NUMBER,
MON_HRS_2 NUMBER,
TUE_HRS_2 NUMBER,
WED_HRS_2 NUMBER,
THUR_HRS_2 NUMBER,
FRI_HRS_2 NUMBER,
SAT_HRS_2 NUMBER,
SUN_HRS_2 NUMBER,
CREATE_DATE DATETIME,
USER_ID VARCHAR2
EMPLOYEE_TABLE
EMPLOYEE_NAME,
EMPLOYEE_NUMBER,
EE_ID,
DEPARTMENT,
SSN
答案 0 :(得分:0)
想出办法来做到这一点。我基本上用数学来计算14天的小时数并通过子查询加入结果集。然后在我的SELECT中使用Case语句和嵌套case语句来选择要设置的工作日值的来源。
讨厌不止一次加入一张桌子,但这似乎最适合我需要它做的事情。
在很高的水平上,它看起来像这样:
SELECT
CLIENT_ID,
CASE WHEN PT_WK_HRS.TOTAL > 0 THEN
CASE WHEN SEC_WK_HRS.TOTAL >0 THEN ST.MON_HRS_1
ELSE CD.MON_HRS_1
END
ELSE PT.MON_HRS_1
END AS WS_MON_HRS_1,
CASE WHEN PT_WK_HRS.TOTAL > 0 THEN
CASE WHEN SEC_WK_HRS.TOTAL >0 THEN ST.TUE_HRS_1
ELSE CD.TUE_HRS_1
END
ELSE PT.TUE_HRS_1
END AS WS_TUE_HRS_1 ----AND SO ON FOR EACH DAY OF THE WEEK
FROM PRIMARY TABLE PT
JOIN
(SELECT PT.CLIENT_ID.
SUM (NVL(MON_HRS_1,0) + NVL(TUE_HRS_1,0) + NVL(WED_HRS_1,0) + NVL(THUR_HRS_1,0) + NVL(FRI_HRS_1,0) +
NVL(SAT_HRS_1,0) + NVL(SUN_HRS_1,0) + NVL(MON_HRS_2,0) + NVL(TUE_HRS_2,0) + NVL(WED_HRS_2,0) +
NVL(THUR_HRS_2,0) + NVL(FRI_HRS_2,0) + NVL(SAT_HRS_2,0) + NVL(SUN_HRS_2,0)) AS TOTAL
FROM PRIMARY_TABLE
GROUP BY CLIENT_ID) AS PT_WK_HRS
ON PT.CLIENT_ID = PT_WK_HRS. CLIENT_ID
JOIN SECONDARY_TABLE ST
ON PT.CLIENT_ID = ST.CLIENT_ID
JOIN
( SELECT PT.CLIENT_ID.
SUM (NVL(MON_HRS_1,0) + NVL(TUE_HRS_1,0) + NVL(WED_HRS_1,0) + NVL(THUR_HRS_1,0) + NVL(FRI_HRS_1,0) +
NVL(SAT_HRS_1,0) + NVL(SUN_HRS_1,0) + NVL(MON_HRS_2,0) + NVL(TUE_HRS_2,0) + NVL(WED_HRS_2,0) +
NVL(THUR_HRS_2,0) + NVL(FRI_HRS_2,0) + NVL(SAT_HRS_2,0) + NVL(SUN_HRS_2,0)) AS TOTAL
FROM SECONDARY_TABLE
GROUP BY CLIENT_ID) AS SEC_WK_HRS
ON PT.CLIENT_ID = SEC_WK_HRS.CLIENT_ID
JOIN CLIENT_DFLT CD
ON PT.CLIENT_ID = CD.CLIENT_ID