Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help Reformating SQL Syntax

Author  Topic 

shyy
Starting Member

3 Posts

Posted - 2013-10-21 : 23:33:44
Hi everyone,

I am having trouble reformatting this older syntax to the newer syntax.
For questions 6 and 7, how would I reformat this to match this type of format?

SELECT Employee_NameFROM EMPLOYEE_T e,
EMPLOYEE_SKILLS_T s,
SKILL_T t
WHERE e.Employee_ID = s.Employee_Id
AND s.Skill_Id = t.Skill_Id
AND t.Skill_Description = '12in Band Saw'
AND e.Employee_ID <> s.Employee_Id
AND s.Skill_Id = t.Skill_Id
AND t.Skill_Description <> '12in Band Saw'

Thank You

http://imageshack.us/photo/my-images/853/mz34.jpg/

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-10-22 : 00:19:25
The link which you shared is not opened for me...

Any way, try this? ( rectified some of typos in the above query)

SELECT Employee_Name
FROM EMPLOYEE_T e, EMPLOYEE_SKILLS_T s, SKILL_T t
WHERE e.Employee_ID = s.Employee_Id AND s.Skill_Id = t.Skill_Id AND t.Skill_Description = '12in Band Saw'
AND e.Employee_ID <> s.Employee_Id
AND s.Skill_Id = t.Skill_Id
AND t.Skill_Description <> '12in Band Saw'


The highlighted part is fully opposite to other conditions available in the above query...

--
Chandu
Go to Top of Page

shyy
Starting Member

3 Posts

Posted - 2013-10-22 : 00:53:30
Thanks for the reply,

The highlighted part is another condition since I need to say which Employee only has one skills where as the one you selected shows employees that have multiple skills.

Please check out this url http://imageshack.us/f/853/mz34.jpg/

This is the correct code, I just prefer this to look like the format above.

SELECT Employee_Name
FROM EMPLOYEE_T
WHERE Employee_ID IN (SELECT Employee_Id
FROM EMPLOYEE_SKILLS_T
WHERE Skill_Id IN (SELECT Skill_Id
FROM SKILL_T
WHERE Skill_Description = '12in Band Saw'))
AND
Employee_ID NOT IN (SELECT Employee_Id
FROM EMPLOYEE_SKILLS_T
WHERE Skill_Id IN (SELECT Skill_Id
FROM SKILL_T
WHERE Skill_Description <> '12in Band Saw'))




Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-10-22 : 02:38:02
-- Alternate

SELECT Employee_Name, Employee_id
FROM EMPLOYEE_T
WHERE Employee_id IN
(SELECT est.Employee_Id
FROM EMPLOYEE_SKILLS_T est JOIN SKILL_T st ON est.Skill_Id = st.Skill_Id
GROUP BY est.Employee_Id
HAVING SUM( CASE WHEN st.Skill_Description = '12in Band Saw' then 0 else 1 end ) = 0 )


If the above is NOT, post us back the result of the below query
SELECT est.Employee_Id
FROM EMPLOYEE_SKILLS_T est JOIN SKILL_T st ON est.Skill_Id = st.Skill_Id
GROUP BY est.Employee_Id
HAVING SUM( CASE WHEN st.Skill_Description = '12in Band Saw' then 0 else 1 end ) = 0

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-22 : 08:08:20
[code]
SELECT e.Employee_Name, e.Employee_id
FROM EMPLOYEE_T e
INNER JOIN (SELECT est.Employee_Id
FROM EMPLOYEE_SKILLS_T est
JOIN SKILL_T st
ON est.Skill_Id = st.Skill_Id
GROUP BY est.Employee_Id
HAVING MIN(st.Skill_Description) = MAX(st.Skill_Description)
AND MIN(st.Skill_Description)='12in Band Saw')e1
ON e1.Employee_Id = e.Employee_Id
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

shyy
Starting Member

3 Posts

Posted - 2013-10-22 : 10:09:06
Thank You guys both worked!

Bandi:
What does this do?
HAVING SUM( CASE WHEN st.Skill_Description = '12in Band Saw' then 0 else 1 end ) = 0 )

visakh16:
Why did you use HAVING MIN = MAX and then use MIN and e1 at the end??

The professor gave us an SQL assignment but never showed us how to actually use it..

Thanks again for the help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-22 : 10:10:50
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-22 : 14:34:40
quote:
Originally posted by shyy

Thank You guys both worked!

Bandi:
What does this do?
HAVING SUM( CASE WHEN st.Skill_Description = '12in Band Saw' then 0 else 1 end ) = 0 )

visakh16:
Why did you use HAVING MIN = MAX and then use MIN and e1 at the end??

The professor gave us an SQL assignment but never showed us how to actually use it..

Thanks again for the help.


MIN = MAX was to ensure you've only one unique Skill_Description available for that employee. Second condition will ensure value is '12in Band Saw' which is what you wanted
e1 is alias ie name given to derived table which contains the query ie query behaving like another table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -