在SQL Server(2008)中,我想查找所有员工在一张表中具有3,4,5或4,5,6的技能。他们需要其中之一。例如,只有skillid = 4,不应该产生匹配。我如何构建这种类型的查询?在单个表中查找多个ID匹配
一个实例表:
pkid, empid, skillid
1 2 3
2 2 4
3 2 5
4 5 6
在上述例子中,EMPID = 2是对于该组3,4,5的匹配。 empid = 5不是。
在SQL Server(2008)中,我想查找所有员工在一张表中具有3,4,5或4,5,6的技能。他们需要其中之一。例如,只有skillid = 4,不应该产生匹配。我如何构建这种类型的查询?在单个表中查找多个ID匹配
一个实例表:
pkid, empid, skillid
1 2 3
2 2 4
3 2 5
4 5 6
在上述例子中,EMPID = 2是对于该组3,4,5的匹配。 empid = 5不是。
您将需要使用一个GROUP BY
和HAVING
子句查询:
select empid
from t1
where skillid in (3, 4, 5)
or skillid in (4, 5, 6)
group by empid
having count(distinct skillid) = 3
尝试
select pkid, empid
from your_table
where skillid in (3,4,5) or skillid in (4,5,6)
group by pkid, empid
having count(distinct skillid) = 3
前两次的答案从无法区分受苦在(1,2,6)或(1,5,6)或(2,5,6)等中设置成员。结果必须仅显示作为ALL的成员的empid, 2,3或4,5,6。
尝试:
create table Table1 (pkid int constraint PK_Table1 primary key, empid int, skillid int) insert into table1 values (1,2,1) insert into table1 values (2,2,2) insert into table1 values (3,2,3) insert into table1 values (4,3,1) SELECT empid FROM ( SELECT empid, sum(t.s1) as s1, sum(t.s2) as s2, sum(t.s3) as s3, sum(t.s4) as s4, sum(t.s5) as s5, sum(t.s6) as s6 FROM ( select empid, 1 s1, 0 s2, 0 s3, 0 s4, 0 s5, 0 s6 from table1 where skillid = 1 union all select empid, 0, 1, 0, 0, 0, 0 from table1 where skillid = 2 union all select empid, 0, 0, 1, 0, 0, 0 from table1 where skillid = 3 union all select empid, 0, 0, 0, 1, 0, 0 from table1 where skillid = 4 union all select empid, 0, 0, 0, 0, 1, 0 from table1 where skillid = 5 union all select empid, 0, 0, 0, 0, 0, 1 from table1 where skillid = 6 ) t GROUP BY t.empid ) tt WHERE (tt.s1 = 1 and tt.s2 = 1 and tt.s3 = 1) or (tt.s4 = 1 and tt.s5=1 and tt.s6=1)
EMPID IN(3,4,5)是简写EMPID = 3或EMPID = 4或EMPID = 5,所以EMPID IN 3,4,5,OR IN EMPID( 4,5,6)与empid IN(3,4,5,6)相同。但是,我们需要避免用3,5,6或3,4,6来计算人数。你可以做这样的事情:如果你愿意,看看哪些EMPID的有两套
SELECT empid, 345 AS skillset
FROM your_table
WHERE skillid IN (3,4,5)
GROUP BY empid
HAVING COUNT(DISTINCT skillid) = 3
UNION ALL
SELECT empid, 456 AS skillset
FROM your_table
WHERE skillid IN (4,5,6)
GROUP BY empid
HAVING COUNT(DISTINCT skillid) = 3
你可以把一个SELECT解决这个问题,等
我你原来的问题的解读是,要找到那些谁满足以下任一条件的员工:
(或两者)。如果这种情况下,你会想用相关子查询做这样的事情:
select *
from employee e
where ( exists (select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 3)
and exists (select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 4)
and exists (select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 5)
)
OR ( exists (select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 4)
and exists (select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 5)
and exists (select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 6)
)
然而,由于你的两个目标的技能集{3,4,5}和{4,5,6}有公共子集{4,5},我们可以简化。重构,我们得到
select *
from employee e
where exists (select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 4 )
and exists (select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 5 )
and exists (select * from employee_skill es3 on es3.empid = e.empid and es3.skillid in (3 , 6))
另一种方法是使用left join
:
select *
from employee e
left join employee_skill es3 on es3.empid = e.empid and es3.skillid = 3
left join employee_skill es4 on es4.empid = e.empid and es4.skillid = 4
left join employee_skill es5 on es5.empid = e.empid and es5.skillid = 5
left join employee_skill es6 on es6.empid = e.empid and es6.skillid = 6
where es4.empid is not null
and es5.empid is not null
and ( es3.empid is not null
OR es6.empid is not null
)
使用left join
后一种方法包含一个隐含的假设,某个员工/技能组合是在数据模型中是唯一的。如果情况并非如此,那么这种方法将需要使用select distinct
以免在结果集中出现重复行。