我是Oracle SQL的新手,想知道是否有人有任何关于如何执行以下操作的示例:
我想返回下面列出的以下SQL语句中存在的行数。这个SQL将成为更大的SQL语句的一部分,我将把EMPLID标识符传递给子查询。结果必须是一个值,显示与下面的SQL匹配的记录总数。 'xyz'最终将使用父SQL的引用传递员工的工作职位编号...即:jbe.POSITION_NBR
我的SQL:
从PS_JOB中选择cjbe.EMPLID cjbe
在哪里cjbe.POSITION_NBR ='xyz'
GROUP BY cjbe.EMPLID
我想知道如何将POSITION_NBR(我想我使用jbe.POSITION_NBR)的poarent值传递到我的子查询中,然后返回在该子查询中找到的EMPLID的总数。
感谢您的帮助。
原始父SQL在这里:
SELECT jbe.EMPLID "Employee_ID", REPLACE(nam.NAME,',',', ') "Name", jbe.HR_STATUS "HR_Status" , jbe.REG_TEMP "Reg/Temp" ,jbe.FULL_PART_TIME "FT/PT" ,jbe.SAL_ADMIN_PLAN "Emp_Type" ,jbe.DEPTID "Dept_ID" ,dpt.descr "Dept_Name" ,
(SELECT Min(pj.EFFDT) AS HIRE_DT_1
FROM PS_JOB pj
WHERE pj.EMPLID=jbe.emplid
AND action IN('HIR',
'REH')
AND pj.empl_rcd = 0) "Emp_Hired_Into_Pos_Dt" , jbe.POSITION_NBR "Position_Num" ,
(SELECT MIN(EFFDT)
FROM PS_JOB
WHERE 1=1
AND POSITION_NBR = jbe.POSITION_NBR) "Position_Orig_Created_On" , pos.DESCR "Position_Job_Title" ,dist.ACCT_CD "Budget" ,dist.DIST_PCT "Distribution" ,
(SELECT COUNT(*)
FROM
(SELECT cjbe.EMPLID
FROM PS_JOB cjbe
WHERE cjbe.POSITION_NBR = jbe.POSITION_NBR
GROUP BY cjbe.EMPLID)) "Qty_In_Position?"
FROM PS_JOB jbe,
PS_NAMES nam,
PS_JOB_EARNS_DIST dist,
PS_POSITION_DATA pos,
ps_dept_tbl dpt
WHERE (dist.EMPLID = jbe.EMPLID
AND dist.EMPL_RCD = jbe.EMPL_RCD
AND dist.EFFDT = jbe.EFFDT
AND dist.EFFSEQ = jbe.EFFSEQ
AND (jbe.EFFDT =
(SELECT MAX(A_ED.EFFDT)
FROM PS_JOB A_ED
WHERE jbe.EMPLID = A_ED.EMPLID
AND jbe.EMPL_RCD = A_ED.EMPL_RCD
AND A_ED.EFFDT <= SYSDATE)
AND jbe.EFFSEQ =
(SELECT MAX(A_ES.EFFSEQ)
FROM PS_JOB A_ES
WHERE jbe.EMPLID = A_ES.EMPLID
AND jbe.EMPL_RCD = A_ES.EMPL_RCD
AND jbe.EFFDT = A_ES.EFFDT)
AND jbe.EMPL_RCD = 0
AND jbe.HR_STATUS = 'A'
AND REGEXP_LIKE (SUBSTR(jbe.POSITION_NBR,1,2), '^S[0-9]')
AND jbe.EMPLID = nam.EMPLID
AND nam.EFFDT =
(SELECT MAX(B_ED.EFFDT)
FROM PS_NAMES B_ED
WHERE nam.EMPLID = B_ED.EMPLID
AND nam.NAME_TYPE = B_ED.NAME_TYPE
AND B_ED.EFFDT <= SYSDATE)
AND nam.NAME_TYPE = 'PRI'))
AND pos.position_nbr(+) = jbe.position_nbr
AND pos.effdt=
(SELECT max(p2.effdt)
FROM ps_position_data p2
WHERE p2.position_nbr=pos.position_nbr
AND p2.effdt<=sysdate)
AND dpt.deptid(+) = jbe.deptid
AND dpt.setid(+) = jbe.setid_dept
AND (dpt.EFFDT=
(SELECT MAX(d2.EFFDT)
FROM PS_DEPT_TBL d2
WHERE dpt.DEPTID=d2.DEPTID
AND jbe.SETID_DEPT=d2.SETID
AND d2.EFFDT<=SYSDATE))
AND (
(SELECT MIN(EFFDT)
FROM PS_JOB
WHERE 1=1
AND POSITION_NBR = jbe.POSITION_NBR) BETWEEN TO_DATE ('01-JAN-2014', 'DD-MON-YYYY') AND TO_DATE ('31-DEC-2014', 'DD-MON-YYYY')
OR
(SELECT Min(pj.EFFDT) AS HIRE_DT_1
FROM PS_JOB pj
WHERE pj.EMPLID=jbe.emplid
AND action IN('HIR',
'REH')
AND pj.empl_rcd = 0) BETWEEN TO_DATE ('01-JAN-2014', 'DD-MON-YYYY') AND TO_DATE ('31-DEC-2014', 'DD-MON-YYYY'))
ORDER BY jbe.POSITION_NBR
答案 0 :(得分:1)
问题在于您的父SQL:
(SELECT COUNT(*)
FROM
(SELECT cjbe.EMPLID
FROM PS_JOB cjbe
WHERE cjbe.POSITION_NBR = jbe.POSITION_NBR
GROUP BY cjbe.EMPLID)) "Qty_In_Position?"
jbe.POSITION_NBR不可用,因为您位于第二个嵌套子查询中。它可以升级一级(在第一个子查询中,你有SELECT COUNT(*))。
父和子查询应如下所示:
SELECT jbe.EMPLID "Employee_ID", REPLACE(nam.NAME,',',', ') "Name", jbe.HR_STATUS "HR_Status" , jbe.REG_TEMP "Reg/Temp" ,jbe.FULL_PART_TIME "FT/PT" ,jbe.SAL_ADMIN_PLAN "Emp_Type" ,jbe.DEPTID "Dept_ID" ,dpt.descr "Dept_Name" ,
(SELECT Min(pj.EFFDT) AS HIRE_DT_1
FROM PS_JOB pj
WHERE pj.EMPLID=jbe.emplid
AND action IN('HIR',
'REH')
AND pj.empl_rcd = 0) "Emp_Hired_Into_Pos_Dt" , jbe.POSITION_NBR "Position_Num" ,
(SELECT MIN(EFFDT)
FROM PS_JOB
WHERE 1=1
AND POSITION_NBR = jbe.POSITION_NBR) "Position_Orig_Created_On" , pos.DESCR "Position_Job_Title" ,dist.ACCT_CD "Budget" ,dist.DIST_PCT "Distribution" ,
(SELECT count(distinct cjbe.EMPLID)
FROM PS_JOB cjbe
WHERE cjbe.POSITION_NBR = jbe.POSITION_NBR) "Qty_In_Position?"
FROM PS_JOB jbe,
PS_NAMES nam,
PS_JOB_EARNS_DIST dist,
PS_POSITION_DATA pos,
ps_dept_tbl dpt
WHERE (dist.EMPLID = jbe.EMPLID
AND dist.EMPL_RCD = jbe.EMPL_RCD
AND dist.EFFDT = jbe.EFFDT
AND dist.EFFSEQ = jbe.EFFSEQ
AND (jbe.EFFDT =
(SELECT MAX(A_ED.EFFDT)
FROM PS_JOB A_ED
WHERE jbe.EMPLID = A_ED.EMPLID
AND jbe.EMPL_RCD = A_ED.EMPL_RCD
AND A_ED.EFFDT <= SYSDATE)
AND jbe.EFFSEQ =
(SELECT MAX(A_ES.EFFSEQ)
FROM PS_JOB A_ES
WHERE jbe.EMPLID = A_ES.EMPLID
AND jbe.EMPL_RCD = A_ES.EMPL_RCD
AND jbe.EFFDT = A_ES.EFFDT)
AND jbe.EMPL_RCD = 0
AND jbe.HR_STATUS = 'A'
AND REGEXP_LIKE (SUBSTR(jbe.POSITION_NBR,1,2), '^S[0-9]')
AND jbe.EMPLID = nam.EMPLID
AND nam.EFFDT =
(SELECT MAX(B_ED.EFFDT)
FROM PS_NAMES B_ED
WHERE nam.EMPLID = B_ED.EMPLID
AND nam.NAME_TYPE = B_ED.NAME_TYPE
AND B_ED.EFFDT <= SYSDATE)
AND nam.NAME_TYPE = 'PRI'))
AND pos.position_nbr(+) = jbe.position_nbr
AND pos.effdt=
(SELECT max(p2.effdt)
FROM ps_position_data p2
WHERE p2.position_nbr=pos.position_nbr
AND p2.effdt<=sysdate)
AND dpt.deptid(+) = jbe.deptid
AND dpt.setid(+) = jbe.setid_dept
AND (dpt.EFFDT=
(SELECT MAX(d2.EFFDT)
FROM PS_DEPT_TBL d2
WHERE dpt.DEPTID=d2.DEPTID
AND jbe.SETID_DEPT=d2.SETID
AND d2.EFFDT<=SYSDATE))
AND (
(SELECT MIN(EFFDT)
FROM PS_JOB
WHERE 1=1
AND POSITION_NBR = jbe.POSITION_NBR) BETWEEN TO_DATE ('01-JAN-2014', 'DD-MON-YYYY') AND TO_DATE ('31-DEC-2014', 'DD-MON-YYYY')
OR
(SELECT Min(pj.EFFDT) AS HIRE_DT_1
FROM PS_JOB pj
WHERE pj.EMPLID=jbe.emplid
AND action IN('HIR',
'REH')
AND pj.empl_rcd = 0) BETWEEN TO_DATE ('01-JAN-2014', 'DD-MON-YYYY') AND TO_DATE ('31-DEC-2014', 'DD-MON-YYYY'))
ORDER BY jbe.POSITION_NBR
答案 1 :(得分:1)
假设您将传入EMPLID和POSITION_NBR,您只需要: SELECT count(EMPLID)FROM PS_JOB cjbe 在哪里cjbe.POSITION_NBR ='xyz' AND cjbe.EMPLID = EMPLID(这是您传递给子查询的内容)
这是一个例子 选择a.EMPLID,a.POSITION_NBR, - 这部分是你的子查询: (SELECT count(EMPLID)FROM PS_JOB cjbe 在哪里cjbe.POSITION_NBR = a.POSITION_NBR AND cjbe.EMPLID = a.EMPLID)count_of_emplid 来自