我有以下表格:
1- EMP_INFO_STATUS (main table)
2- NEW_EMP_PERSONAL
3- NEW_EMP_NOTE
4- NEW_EMP_CRS
5- NEW_EMP_CER
6- EMPLOYEES
我想要sql语句
返回表EMP_INFO_STATUS中的所有记录,如果:
这个sql不起作用
SELECT A.EMP_NO,A.STATUS,
B.EMPLOYEE_NO,B.EMP_1ST_NAME,B.EMP_2ND_NAME,
B.EMP_3RD_NAME,B.EMP_4TH_NAME
FROM p9.EMP_INFO_STATUS A,
p9.EMPLOYEES B,
p9.NEW_EMP_PERSONAL C,
p9.NEW_EMP_NOTE D,
p9.NEW_EMP_CRS E,
p9.NEW_EMP_CER F
WHERE
A.EMP_NO = B.EMPLOYEE_NO
AND A.EMP_NO = C.EMP_NO(+)
AND A.EMP_NO = C.EMP_NO(+)
AND A.EMP_NO = E.EMP_NO(+)
AND A.EMP_NO = F.EMP_NO(+)
AND A.STATUS = 2
AND (
C.SECONDMENT_IMAGE is not NULL
OR C.NOTES is not NULL
OR D.NOTE is not NULL
OR E.EMP_NO IS NOT NULL
OR F.EMP_NO IS NOT NULL
)
GROUP BY A.EMP_NO, A.STATUS, B.EMPLOYEE_NO,
B.EMP_1ST_NAME,B.EMP_2ND_NAME,
B.EMP_3RD_NAME,B.EMP_4TH_NAME
答案 0 :(得分:0)
尝试类似未经测试的select语句:
SELECT InnerQuery.EMP_NO
, InnerQuery.STATUS
, Employees.EMPLOYEE_NO
, Employees.EMP_1ST_NAME
, Employees.EMP_2ND_NAME
, Employees.EMP_3RD_NAME
, Employees.EMP_4TH_NAME
FROM
(
SELECT A.EMP_NO
, A.STATUS
, B.EMPLOYEE_NO
, COUNT(B.EMPLOYEE_NO) +
COUNT(C.EMPLOYEE_NO) +
COUNT(D.EMPLOYEE_NO) +
COUNT(E.EMPLOYEE_NO) +
COUNT(F.EMPLOYEE_NO) as subrecords
FROM p9.EMP_INFO_STATUS A
LEFT JOIN p9.EMPLOYEES B
ON B.EMPLOYEE_NO = A.EMP_NO
AND B.SECONDMENT_IMAGE IS NOT NULL
LEFT JOIN p9.NEW_EMP_PERSONAL C
ON C.EMP_NO = A.EMP_NO
LEFT JOIN p9.NEW_EMP_NOTE D
ON D.EMP_NO = A.EMP_NO
LEFT JOIN p9.NEW_EMP_CRS E
ON E.EMP_NO = A.EMP_NO
LEFT JOIN p9.NEW_EMP_CER F
ON F.EMP_NO = A.EMP_NO
WHERE A.STATUS = 2
GROUP BY A.EMP_NO
HAVING subrecords > 0
-- this may need to be: HAVING COUNT(B.EMPLOYEE_NO) + COUNT(C.EMPLOYEE_NO) + COUNT(D.EMPLOYEE_NO) + COUNT(E.EMPLOYEE_NO) + COUNT(F.EMPLOYEE_NO) > 0
)
LEFT JOIN p9.EMPLOYEES Employees
ON Employees.EMPLOYEE_NO = InnerQuery.EMP_NO;