我正在对此查询进行操作,试图使其仅返回将传递给外部查询的一列。问题是如何重写此查询,使其只返回一列,然后再将其加入主查询中,以消除上述错误?
(
SELECT *
FROM (
SELECT DISTINCT JIN.TITLE,
RANK () OVER (PARTITION BY EIN.PI_CANDIDATE_NUM
ORDER BY EIN.PI_HIS_ITM_APP_TRK_CREATION_DT DESC) AS ROWRANK
FROM WC_HIRED_INTERNS_FA HIN
INNER JOIN WC_APPLICATION_EVENT_F EIN
ON HIN.PI_CANDIDATE_NUM = EIN.PI_CANDIDATE_NUM
INNER JOIN WC_JOB_INFORMATION_D JIN
ON EIN.JOB_INFO_ROW_WID = JIN.ROW_WID
WHERE EIN.PI_HIS_ITM_APP_TRK_STS_NAME = 'Hired - External'
AND EIN.PI_HIS_ITM_APP_TRK_STEP_NAME = 'Hire'
AND JIN.JOB_FAMILY_NAME IN ('MDP', 'ELP', 'Emerging Leader Program', 'Other')
AND JIN.TITLE NOT IN ('Student Ambassador Program for Eligible Summer Interns',
'Student Ambassador')
AND HIN.PI_CANDIDATE_NUM = H.PI_CANDIDATE_NUM
) ac
where ROWRANK = 1
) AS ACCEPTEDTITLE
尝试应用Aleksj的建议后,完成代码:
SELECT DISTINCT H.PI_CANDIDATE_NUM,
H.PARENT_PI_NUMBER,
H.ASSOCIATE_NAME,
J.JOB_FAMILY_NAME,
J.ORGANIZATION_NAME,
J.TITLE,
E.CONTEST_NUMBER,
E.PI_HIS_ITM_APP_TRK_CREATION_DT OFFER_DATE,
(
SELECT JIN.TITLE
FROM (
SELECT DISTINCT RANK () OVER (PARTITION BY EIN.PI_CANDIDATE_NUM
ORDER BY EIN.PI_HIS_ITM_APP_TRK_CREATION_DT DESC) AS ROWRANK
FROM WC_HIRED_INTERNS_FA HIN
INNER JOIN WC_APPLICATION_EVENT_F EIN ON HIN.PI_CANDIDATE_NUM = EIN.PI_CANDIDATE_NUM
INNER JOIN WC_JOB_INFORMATION_D JIN ON EIN.JOB_INFO_ROW_WID = JIN.ROW_WID
WHERE JIN.JOB_FAMILY_NAME IN ('MDP', 'ELP', 'Emerging Leader Program', 'Other')
AND EIN.PI_HIS_ITM_APP_TRK_STS_NAME = 'Hired - External'
AND EIN.PI_HIS_ITM_APP_TRK_STEP_NAME = 'Hire'
AND JIN.TITLE NOT IN ('Student Ambassador Program for Eligible Summer Interns',
'Student Ambassador')
AND HIN.PI_CANDIDATE_NUM = H.PI_CANDIDATE_NUM
) ac
where ROWRANK = 1
) AS ACCEPTEDTITLE
FROM WC_HIRED_INTERNS_FA H
INNER JOIN WC_APPLICATION_EVENT_F E ON H.PI_CANDIDATE_NUM = E.PI_CANDIDATE_NUM
INNER JOIN WC_JOB_INFORMATION_D J ON E.JOB_INFO_ROW_WID = J.ROW_WID
WHERE E.PI_HIS_ITM_APP_TRK_CREATION_DT IN (
SELECT DISTINCT E.PI_HIS_ITM_APP_TRK_CREATION_DT --limit 1
FROM WC_APPLICATION_EVENT_F E
INNER JOIN WC_HIRED_INTERNS_FA H ON H.PI_CANDIDATE_NUM = E.PI_CANDIDATE_NUM
INNER JOIN WC_JOB_INFORMATION_D J ON E.JOB_INFO_ROW_WID = J.ROW_WID
WHERE J.JOB_FAMILY_NAME IN ('MDP', 'ELP', 'Emerging Leader Program', 'Other')
AND E.PI_HIS_ITM_APP_TRK_STS_NAME = 'Extended'
AND E.PI_HIS_ITM_APP_TRK_STEP_NAME = 'Offer'
AND J.TITLE NOT IN ('Student Ambassador Program for Eligible Summer Interns',
'Student Ambassador')
);
答案 0 :(得分:0)
正如Aleksej在评论中所说,您需要将*
更改为title
;根据您以后的评论,您似乎已经将jin.title
引用完全移出了内部查询,并保留了前缀。
所以不是
(
SELECT *
FROM (
SELECT DISTINCT JIN.TITLE,
RANK () OVER (PARTITION BY EIN.PI_CANDIDATE_NUM
ORDER BY EIN.PI_HIS_ITM_APP_TRK_CREATION_DT DESC) AS ROWRANK
...
) ac
where ROWRANK = 1
) AS ACCEPTEDTITLE
或您的修改版本:
(
SELECT JIN.TITLE
FROM (
SELECT DISTINCT RANK () OVER (PARTITION BY EIN.PI_CANDIDATE_NUM
ORDER BY EIN.PI_HIS_ITM_APP_TRK_CREATION_DT DESC) AS ROWRANK
...
) ac
where ROWRANK = 1
) AS ACCEPTEDTITLE
您需要:
(
SELECT TITLE
FROM (
SELECT DISTINCT
JIN.TITLE,
RANK () OVER (PARTITION BY EIN.PI_CANDIDATE_NUM
ORDER BY EIN.PI_HIS_ITM_APP_TRK_CREATION_DT DESC)AS ROWRANK
...
) ac
where ROWRANK = 1
) AS ACCEPTEDTITLE
我已经评论了您以前的问题,我认为您需要重新评估和简化对子查询和重复代码的使用。 DISTINCT
的使用通常表示有些不对劲。
您仍然没有提供MCVE,但是在对您正在做的事情和您的数据进行一些假设的情况下,您似乎想要以下内容:
select h.pi_candidate_num,
h.parent_pi_number,
h.associate_name,
j.job_family_name,
j.organization_name,
j.title,
e.contest_number,
max(case when e.pi_his_itm_app_trk_sts_name = 'Extended'
and e.pi_his_itm_app_trk_step_name = 'Offer'
then e.pi_his_itm_app_trk_creation_dt end) as offer_date,
max(case when e.pi_his_itm_app_trk_sts_name = 'Hired - External'
and e.pi_his_itm_app_trk_step_name = 'Hire'
then e.pi_his_itm_app_trk_creation_dt end) as accepted_title,
max(case when e.pi_his_itm_app_trk_sts_name = 'Hired - External'
and e.pi_his_itm_app_trk_step_name = 'Hire'
then e.pi_his_itm_app_trk_creation_dt end) as hire_date
from wc_hired_interns_fa h
join wc_application_event_f e on h.pi_candidate_num = e.pi_candidate_num
join wc_job_information_d j on e.job_info_row_wid = j.row_wid
where j.job_family_name in ('MDP', 'ELP', 'Emerging Leader Program', 'Other')
and (
(e.pi_his_itm_app_trk_sts_name = 'Extended'
and e.pi_his_itm_app_trk_step_name = 'Offer')
or (e.pi_his_itm_app_trk_sts_name = 'Hired - External'
and e.pi_his_itm_app_trk_step_name = 'Hire')
)
and j.title not in ('Student Ambassador Program for Eligible Summer Interns',
'Student Ambassador')
group by h.pi_candidate_num,
h.parent_pi_number,
h.associate_name,
j.job_family_name,
j.organization_name,
j.title,
e.contest_number;
我认为应该显示扩展给候选人的所有提议,并针对他们接受的提议(如果有的话)重复该标题(这似乎没什么用,但这是您的子查询正在寻找的东西) ),并且可能有用的聘用日期。
当然未经测试,但可能会为您提供获得所需东西的新方向。