--方法一
--用时: 00: 00: 29.09
select b.m_no,b.sq,b.p_roll,b.npl,b.tr_date from t_tr b,
(select count(*),m_no,sq,p_roll,npl
from t_tr
group by m_no,sq,p_roll,npl
having count(*)>1) a
where b.m_no=a.m_no and b.sq=a.sq and b.p_roll=a.p_roll and b.npl=a.npl
order by b.tr_date
--方法二
--用时:n秒,最后只好卡叉掉
select m_no,sq,p_roll,npl,tr_date
from t_tr a
where a.rowid!=(select max(rowid)
from t_tr b
where a.m_no=b.m_no and a.sq=b.sq and a.p_roll=b.p_roll and a.npl=b.npl)
--方法三
--用时:5秒,
--使用oracle的分析函数
select M_NO,SQ,PT,P_ROLL,P_YDS,P_KGS,tr_date,dept from TR
where (M_NO,SQ,nvl(PT,'
(select M_NO,SQ,Nvl(PT,'
(select M_NO,SQ,PT,P_ROLL,Row_Number() over (partition By M_NO,SQ,PT,P_ROLL order by M_NO,SQ,PT,P_ROLL) rn
from tr order by M_NO,SQ,PT,P_ROLL) t
where rn>1)
and tr_date>to_date('2004-04-01','yyyy-mm-dd')
小结:合理的使用oracle的分析函数可以很大的提高sql的执行效率。
select * from user
where uid not in(select u1.uid from user as u1,user as u2 《==这里面一个not in 就找到了那个最大的uid。 所以你最终结果查出了重复名字中id最大的那个
《===这个select是查出了在当前重复名称下,所有uid1
(select username from user group by username))《==这个查出了所有有重复的名字
这条sql语句终于进化了。嘿嘿。是这样的
select * from
user u1 ,(select min(uid) as minuid,username from user group by username) u2--u2查询出的是将重复的username排掉,并得到该组username中最小的id
where u1.uid=u2.minuid and u1.username=u2.username --id唯一,通过唯一的id 就能得到一条不重复的全记录了。因为我选择的是min(id)id是不重复的,所以可以不写这些。如果是min(email)可能,email会有重复 的,在表连接的时候就要加上where条件中,后面的那个条件
order by u1.uid