Sei sulla pagina 1di 5

7/4/2014 T-SQL Query help needed

United States (English) Sign in


Search SQL Server with Bing

Home Library Learn Downloads Troubleshooting Community Forums

Ask a question Search related threads Search forum questions

Quick access

Answered by: T-SQL Query help needed


SQL Server > Transact-SQL

Question

Hi All,

I need a help in writing a simple SQL for the following requirement. Below is the table design:
Naomi N
Employee(EmpID, Name)
Siriusware 0
Partner Sign in Skill(SkillID, SkillName)
blogs.lessthandot.c... to vote
EmployeeSkills(EmpID, SkillID)
206,645 Points 14 14 24
I am an IT professional with more than 15 Give the list of employees only if they have all the skills specified. For example: Emp 1 has Skill1,
years of experience in variety of Skill2, Skill4 & Emp2 has Skill1, Skill2, Skill3 skills. If we pass Skill1, Skill2, Skill3 as input, only Emp2
programming languages and should be displayed since he has all the skills.
technologies. I am a Microsoft Community
Award Recipient and Personality of the Thanks in Advance !
Year at UniversalThread.com forum in
2008,2009,2010,2011. I am also a TechNet
Guru competition multi times Gold Wednesday, May 28, 2014 4:55 PM
Winner.
Reply | Quote PSK1990 0 Points
Naomi N's threads
View Profile

Answers
Top related threads
Refactor TSQL Query Help Needed
This is a typical relational division problem.
TSQL Query need help
Take a look at this article
T-SQL query... Need help
Newbie Need help with tsql query T-SQL: Relational Division
1
need help with a T-SQL query Sign in select E.* from Employee E inner join EmployeeSkills ES ON E.EmpID = ES.EmpID INNER JOIN Skills
to vote S ON ES.SkillID = S.SkillID

INNER JOIN @ListOfSkills LS ON S.SkillName = LS.SkillName

GROUP BY E.EmpID, E.Name

HAVING COUNT(S.SkillID) = (select count(*) from @ListOfSkills)

For every expert, there is an equal and opposite expert. - Becker's Law

My blog

My TechNet articles

Edited by Naomi N Moderator Wednesday, May 28, 2014 5:13 PM


Marked as answer by Fanny Liu Microsoft contingent staff, Moderator
Thursday, June 05, 2014 12:04 PM

Wednesday, May 28, 2014 5:10 PM

Reply | Quote
Naomi N Siriusware (Partner) 206,645 Points

All replies

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e8e018eb-7ffd-4ff2-afc2-042b384ce1aa/tsql-query-help-needed?forum=transactsql 1/5
7/4/2014 T-SQL Query help needed

This is a typical relational division problem.

Take a look at this article

T-SQL: Relational Division


1
Sign in select E.* from Employee E inner join EmployeeSkills ES ON E.EmpID = ES.EmpID INNER JOIN Skills
to vote S ON ES.SkillID = S.SkillID

INNER JOIN @ListOfSkills LS ON S.SkillName = LS.SkillName

GROUP BY E.EmpID, E.Name

HAVING COUNT(S.SkillID) = (select count(*) from @ListOfSkills)

For every expert, there is an equal and opposite expert. - Becker's Law

My blog

My TechNet articles

Edited by Naomi N Moderator Wednesday, May 28, 2014 5:13 PM


Marked as answer by Fanny Liu Microsoft contingent staff, Moderator
Thursday, June 05, 2014 12:04 PM

Wednesday, May 28, 2014 5:10 PM

Reply | Quote
Naomi N Siriusware (Partner) 206,645 Points

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e8e018eb-7ffd-4ff2-afc2-042b384ce1aa/tsql-query-help-needed?forum=transactsql 2/5
7/4/2014 T-SQL Query help needed

This is a typical relational division problem.

Take a look at this article

0 T-SQL: Relational Division


Sign in
to vote select E.* from Employee E inner join EmployeeSkills ES ON E.EmpID = ES.EmpID INNER JOIN
Skills S ON ES.SkillID = S.SkillID

INNER JOIN @ListOfSkills LS ON S.SkillName = LS.SkillName

GROUP BY E.EmpID, E.Name

HAVING COUNT(S.SkillID) = (select count(*) from @ListOfSkills)

For every expert, there is an equal and opposite expert. - Becker's Law

My blog

My TechNet articles

why joining with skill name? why not this

Select E.* from @employee E


JOIN
@EmployeeSkills ES
ON ES.EmpID=e.EmpID
JOIN
@skill s
ON S.skillID=E.EmpID
group by e.EmpID,e.Name
HAVING COUNT(ES.skillID)=(sELECT COUNT(*) FROM @skill)

- please mark correct answers

Wednesday, May 28, 2014 5:47 PM

Reply | Quote Murali dhar 885 Points

Because I assumed we're passing Skill Names, not IDs from the interface. If we passed Ids, then we
don't need to join with the Skills table.

For every expert, there is an equal and opposite expert. - Becker's Law

1
Sign in
to vote My blog

My TechNet articles

Wednesday, May 28, 2014 6:17 PM

Reply | Quote
Naomi N Siriusware (Partner) 206,645 Points

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e8e018eb-7ffd-4ff2-afc2-042b384ce1aa/tsql-query-help-needed?forum=transactsql 3/5
7/4/2014 T-SQL Query help needed

Doing a raw count of kills/employee doesn't account for data integrity problems in the database.
Say Empl2 has two records in the EmployeeSkills table for Skill1, and raw count would yield 4 and
Emp2 wouldn't be returned. Sure that problem could be resolved by adding a count(distinct
SkillID) but I believe there's a better way to solve the problem:
0
Sign in
declare @Employee table (EmpID int, Name varchar(100))
to vote declare @Skill table (SkillID int, SkillName varchar(100))
declare @EmployeeSkills table (EmpID int, SkillID int)

insert @Employee values (1, 'Fred'), (2, 'Jeff')


insert @Skill values (1, 'Skill1'), (2, 'Skill2'), (3, 'Skill3'), (4, 'Skill4')
insert @EmployeeSkills values (1, 1), (1, 2), (1, 4), (2, 1), (2, 2), (2, 3)

declare @SkillList table (SkillID int)

insert @SkillList values (1), (2), (3)

;with MissingSkills
as
(
select distinct
es.EmpID
from @SkillList as l
right join @EmployeeSkills as es
on l.SkillID = es.SkillID
where l.SkillID is null
)
select e.*
from @Employee as e
left join MissingSkills as m
on m.EmpID = e.EmpID
where m.EmpID is null

When I say better, I'm talking about accuracy. Performance might be a different story.

Edited by Ed at Hammar Technology Thursday, May 29, 2014 1:56 PM

Thursday, May 29, 2014 1:56 PM

Reply | Quote Ed at Hammar Technology Hammar Technology, LLC 1,600 Points

Try this one

declare @SkillList table (SkillID int)


insert @SkillList values (1), (2),(3)

0
Sign in
select emp.EmpID from
to vote Skill sk join EmployeeSkills emp
on sk.SkillID = emp.SkillID
where emp.SkillID in (select SkillID from @SkillList) group by emp.EmpID having count(*) = (select COUNT

Thanks. BalaKrishnan

Tuesday, June 03, 2014 6:34 PM

Reply | Quote Bala - Krishnan 0 Points

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e8e018eb-7ffd-4ff2-afc2-042b384ce1aa/tsql-query-help-needed?forum=transactsql 4/5
7/4/2014 T-SQL Query help needed

Hi,

See http://msdn.microsoft.com/en-us/library/ms178543.aspx

Use the ALL in your query.


0
Sign in
to vote
Tuesday, June 03, 2014 6:38 PM

Reply | Quote Willem Jan Motiv IT Masters (Partner) 0 Points

© 2014 Microsoft. All rights reserved.


Terms of Use | Trademarks | Privacy Statement | Site Feedback

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e8e018eb-7ffd-4ff2-afc2-042b384ce1aa/tsql-query-help-needed?forum=transactsql 5/5

Potrebbero piacerti anche