我有一个查询,会删除重复assignment_name, effective_start_date, effective_end_date, effective_latest_change
的重复行。员工的详细信息有两个表格
xx_as_table :
Ass_nmber eff_s_date eff_e_date effective_l_change special_ceiling date_Start employer_name worker_type
-------------------------------------------------------------------------------------
10228 01-jan-1990 31-dec-4172 1 0 01-feb-1989 XYZ Ltd E
10228 03-feb-1989 31-dec-1989 2 0 01-feb-1989 XYZ Ltd E
xx_wrk_table
ass_number date_Start employer_name worker_type
------------------------------------------------------------
10228 01-feb-1989 XYZ Ltd E
10228 01-jan-1991 ABC Ltd P
现在,我想要一个检索上述数据的查询,如下所示:
Ass_nmber eff_s_date eff_e_date eff_l_change special_ceiling date_Start employer_name worker_type
---------------------------------------------------------------------------------------------
10228 01-jan-1990 31-dec-4172 1 0 01-feb-1989 XYZ Ltd E
10228 03-feb-1989 31-dec-1989 2 0 01-feb-1989 XYZ Ltd E
我想要xx_wrk_table中的date_start和min date_start以及针对min min_Start的详细信息。并且没有重复的行应该具有相同的assignment_name,effective_start_date,effective_end_date,effective_latest_change
为此,我写了一个查询:
select
ass_number,
regexp_substr(ass_number, '[0-9]+') person_number,
effective_start_date,
effective_end_date,
nvl((select min (wrk.date_start)
from
XX_WRK_STG
where
WRK.PERSON_NUMBER = regexp_substr(assignment_number, '[0-9]+')), effective_start_date) D, t.effective_start_date),
nvl(wrk.worker_type, 'E'),
nvl(wrk.employer_name, 'XYZ'),
effective_latest_change
from (SELECT xx_as_table.*,
COUNT(*) OVER (PARTITION BY assignment_name,
effective_start_date,
effective_end_date,
effective_latest_change) AS c
FROM xx_as_table) T
left outer join
XX_WRK_STG wrk
on regexp_substr(t.assignment_name, '[0-9]+')=wrk.person_number
where c=1;
但是这个查询不起作用,并且针对每个ass_number从wrk_table中检索到多个数据。如何修改查询以获得预期结果?
答案 0 :(得分:0)
我无法完全理解您的表结构,因为在您的示例和书面查询中,您使用了不同的列名和一些其他列。
我还是试着帮忙。要查找具有最小日期的工作表中的行,您可以使用Rank function
。
SELECT x_tab.Ass_nmber,
x_tab.eff_s_date,
x_tab.eff_e_date,
x_tab.effective_l_change,
x_tab.special_ceiling,
work_tab.date_Start,
work_tab.employer_name,
work_tab.worker_type
FROM (SELECT Ass_nmber,
eff_s_date,
eff_e_date,
effective_l_change,
special_ceiling,
COUNT(*) OVER (PARTITION BY Ass_nmber,
eff_s_date,
eff_e_date,
effective_l_change) cnt
FROM xx_as_table)x_tab,
(SELECT ass_number,
date_Start,
employer_name,
worker_type,
person_number,
RANK() OVER(PARTITION BY ass_number
ORDER BY date_Start DESC) rnk
FROM xx_wrk_table) work_tab
WHERE s_tab.cnt = 1
AND work_tab.rnk = 1
AND regexp_substr(s_tab.Ass_nmber, '[0-9]+') = work_tab.person_number;
如果它没有帮助,请提供正确的创建和插入语句。这将有助于其他人帮助你。感谢。