我有2个表,即table1和table2。两个表都有关系密钥mer_nbr。但是,表2中的条目是重复的,我们需要找到它们。
示例数据。 表1
colA colB colC mer_nbr
1 01-Apr 03-Apr 1
2 02-Apr 03-Apr 2
3 03-Apr 04-Apr 3
4 04-Apr 05-Apr 4
Table2
colD colE colF mer_nbr delay
1 01-Apr 03-Apr 1 1
2 01-Apr 03-Apr 1 2
3 01-Apr 03-Apr 2 3
4 04-Apr 05-Apr 4 4
我需要的结果是mer_nbr 1,它在table2中有2个条目,具有不同的延迟但是相同的时间段,即01-Apr和03-Apr。
我尝试使用select table2.* from table1, table2 WHERE table2.colE = '01-APR-14' AND table2.colF = '03-APR-14'AND table1.mer_nbr = table2.mer_nbr;
但是,我也得到了mer_nbr = 2的详细信息。我只需要为mer_nbr 1显示2行的结果集。即
colD colE colF mer_nbr delay
1 01-Apr 03-Apr 1 1
2 01-Apr 03-Apr 1 2
请帮助。
答案 0 :(得分:0)
你可以试试这个
with T as
(
select 1 as colD,to_date('01-04-2014','dd-mm-yyyy') as colE,to_date('03-04-2014','dd-mm-yyyy') as colF,1 as mem_nbr,1 as delay from dual
union
select 2 as colD,to_date('01-04-2014','dd-mm-yyyy') as colE,to_date('03-04-2014','dd-mm-yyyy') as colF,1 as mem_nbr,2 as delay from dual
union
select 3 as colD,to_date('01-04-2014','dd-mm-yyyy') as colE,to_date('03-04-2014','dd-mm-yyyy') as colF,2 as mem_nbr,3 as delay from dual
union
select 4 as colD,to_date('04-04-2014','dd-mm-yyyy') as colE,to_date('05-04-2014','dd-mm-yyyy') as colF,4 as mem_nbr,4 as delay from dual
)
select * from
(
select colD,colE,colf,mem_nbr,delay,count(*) over(partition by cole,colf,mem_nbr) as cnt
from T
) where cnt>1