我有 2 张桌子,分别名为
TBL_FIBER_INV_JOBS
和 TBL_FIBER_INV_JOB_PROGRESS
。以下是这两个表的表说明
TBL_FIBER_INV_JOBS (Master Table) & TBL_FIBER_INV_JOB_PROGRESS (Child Table)
TBL_FIBER_INV_JOBS (Master Table)
Name Null? Type
------------------------- -------- --------------
JOB_ID NOT NULL NUMBER
SPAN_ID NVARCHAR2(100)
LINK_ID NVARCHAR2(100)
CREATED_BY NVARCHAR2(200)
CREATED_DATE NOT NULL DATE
MAINTENANCEZONECODE NVARCHAR2(50)
MAINTENANCEZONENAME NVARCHAR2(100)
MAINT_ZONE_NE_SPAN_LENGTH NUMBER(10,4)
SPAN_TYPE NVARCHAR2(20)
JOB_FLAG NUMBER
MISSING_ABD_LENGTH NUMBER(38,10)
REOFFERFLAG VARCHAR2(10)
TBL_FIBER_INV_JOB_PROGRESS (Child Table)
Name Null? Type
---------------------- -------- --------------
JOB_PROGRESS_ID NOT NULL NUMBER
JOB_ID NUMBER
STATUS_ID NUMBER
APPROVED_BY NVARCHAR2(200)
APPROVED_DATE DATE
REJECTED_BY NVARCHAR2(200)
REJECTED_DATE DATE
APPROV_REJECT_REMARK NVARCHAR2(255)
DELAY_REASON NVARCHAR2(255)
ISABDMISSING NUMBER
HOTO_OFFERED_LENGTH NUMBER
LIT_OFFERED_LENGTH NUMBER
HOTO_ACTUAL_LENGTH NUMBER
LIT_ACTUAL_LENGTH NUMBER
ABD_COMPLETED_LENGTH NUMBER
NE_SPAN_LENGTH NUMBER(10,4)
CREATED_BY NVARCHAR2(200)
CREATED_DATE NOT NULL DATE
MODIFIED_BY NVARCHAR2(200)
MODIFIED_DATE DATE
UMS_GROUP_ASS_BY_ID NUMBER
UMS_GROUP_ASS_BY_NAME NVARCHAR2(200)
UMS_GROUP_ASS_TO_ID NUMBER
UMS_GROUP_ASS_TO_NAME NVARCHAR2(200)
UMS_GROUP_ASS_TO_DATE DATE
JOB_PROGRESS_FLAG NOT NULL NUMBER
所以情况是子表中可以有多个具有唯一
JOB_ID
的JOB_PROGESS_ID
。现在我想要的是
一个过程,我总是想要那个
的最大值JOB_PROGESS_ID
,子表的一个条件是JOB_ID
子桌状况:-
UMS_GROUP_ASS_BY_NAME = 'CMM'
和 UMS_GROUP_ASS_TO_NAME IS NULL
。我只想要主表中的 3 列,即 JOB_ID, SPAN_ID, LINK_ID
因为我无法获取特定
JOB_PROGRESS_ID
的最大JOB_ID
。请推荐
更新
我尝试使用以下查询,但它不准确。
select max(job_progress_id),hoto_actual_length from tbl_fiber_inv_job_progress where job_id = 86753 and ums_group_ass_by_name='CMM' and ums_group_ass_to_name is null group by hoto_actual_length;
select max(job_id), span_id, job_flag,nvl(missing_abd_length,0) missing_abd_length, maint_zone_ne_span_length, maintenancezonecode from tbl_fiber_inv_jobs where job_id = 86753 and job_flag = 0 and span_type <> 'FTTX' group by span_id, job_flag,missing_abd_length, maint_zone_ne_span_length,maintenancezonecode; -- 187375
它不是加入(以及聚合和一些过滤器)吗?
SELECT j.job_id,
j.span_id,
j.link_id,
MAX (p.job_progress_id) job_progress_id
FROM tbl_fiber_inv_jobs j JOIN tbl_fiber_inv_job_progress p ON p.job_id = j.job_id
WHERE p.ums_group_ass_by_name = 'CMM'
AND p.ums_group_ass_to_name IS NULL
GROUP BY j.job_id, j.span_id, j.link_id;
或者,也许使用 CTE?
WITH
temp
AS
-- get MAX job_progress_id for every job_id (filter applied)
( SELECT MAX (p.job_progress_id) job_progress_id, p.job_id
FROM tbl_fiber_inv_job_progress p
WHERE p.ums_group_ass_by_name = 'CMM'
AND p.ums_group_ass_to_name IS NULL
GROUP BY p.job_id)
-- get columns from the master table joined with the temp CTE on a common column (job_id)
SELECT j.job_id,
j.span_id,
j.link_id,
t.job_progress_id
FROM tbl_fiber_inv_jobs j JOIN temp t ON t.job_id = j.job_id;