Empid Deptid Email
39 42 uni@mail.com
38 43 uni@mail.com
37 44 uni@mail.com
25 35 unique@maill.com
26 36 second@maill.com
如何找到唯一的记录? empid - > 25和26。
我尝试了以下查询。
select empid,deptid,row_number() over(PARTITION BY lower(email) Order By empid desc) from emp
group by email,empid
答案 0 :(得分:3)
这个怎么样:
select *
from emp
where Email =any (select Email from emp group by Email having count(*) = 1);
我还发现了没有子查询或自联接的版本。至少从理论上来说,它应该具备所有人的最佳表现。
SELECT
MAX(Empid) KEEP (DENSE_RANK FIRST ORDER BY NULL) AS Empid,
MAX(Deptid) KEEP (DENSE_RANK FIRST ORDER BY NULL) AS Deptid,
Email
FROM emp
GROUP BY Email
HAVING COUNT(*) = 1;
答案 1 :(得分:3)
您可以使用COUNT
分析功能。
Oracle 11g R2架构设置:
CREATE TABLE emp
(Empid int, Deptid int, Email varchar2(16))
;
INSERT ALL
INTO emp (Empid, Deptid, Email)
VALUES (39, 42, 'uni@mail.com')
INTO emp (Empid, Deptid, Email)
VALUES (38, 43, 'uni@mail.com')
INTO emp (Empid, Deptid, Email)
VALUES (37, 44, 'uni@mail.com')
INTO emp (Empid, Deptid, Email)
VALUES (25, 35, 'unique@maill.com')
INTO emp (Empid, Deptid, Email)
VALUES (26, 36, 'second@maill.com')
SELECT * FROM dual
;
查询1 :
SELECT Empid, Deptid, Email
FROM (SELECT e.*, COUNT (Empid) OVER (PARTITION BY Email) ct
FROM emp e)
WHERE ct = 1
<强> Results 强>:
| EMPID | DEPTID | EMAIL |
|-------|--------|------------------|
| 26 | 36 | second@maill.com |
| 25 | 35 | unique@maill.com |
答案 2 :(得分:1)
您可以使用not exists
查询来执行此操作:
select e1.*
from emp e1
where not exists (select *
from emp e2
where e2.empid <> e1.empid
and lower(e2.email) = lower(e1.email));
答案 3 :(得分:1)
另一种选择:
select empid
from emp
where email in (select email
from emp
group by email
having count(*) = 1);
答案 4 :(得分:0)
SELECT *
FROM emp
WHERE email IN (SELECT email FROM emp GROUP BY email HAVING COUNT(*)=1)