我想在数据库中找到输入IRN#的副本。以下是IRN的唯一属性(逻辑上唯一)。
ProjectNo, DrawingNo, DrawingRev, SpoolNo, WeldNo
IRN可以有多个WeldNos,这意味着上述唯一属性可以重复一个IRN#(当然,5个属性值中的一个必须是唯一的。)
现在我试图找出是否有任何重复的IRN输入系统?如何通过SQL查询找到它?
P.S:由于数据库设计不好,表中没有主键..
这是我到目前为止所尝试的内容,但这并没有给出正确的结果。
select * from WeldInfo a, WeldInfo b
where a.ProjectNo = b.ProjectNo and
a.DrawingNo = b.DrawingNo and
a.DrawingRev = b.DrawingRev and
a.SpoolNo = b.SpoolNo and
a.WeldNo = b.WeldNo and
a.IrnNo <> b.IrnNo;
答案 0 :(得分:2)
但我不确定,我理解你的问题。
select * from (
select count(*) over ( partition by ProjectNo, DrawingNo, DrawingRev, SpoolNo, WeldN) rr,t.* from WeldInfo t)
where rr > 1;
解释
with tab as (
select 1 as id, 'a' as a , 'b' as b , 'c' as c from dual
union all
select 2 , 'a', 'b', 'c' from dual
union all
select 3 , 'x', 'b', 'c' from dual
union all
select 3 , 'x', 'b', 'c' from dual
union all
select 3 , 'x', 'd', 'c' from dual
)
select t.*
, count(*) over (partition by a,b,c) cnt1
, count(distinct id) over (partition by a,b,c) cnt2
from tab t;