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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 select only non-duplicates

Author  Topic 

lsulindy
Starting Member

9 Posts

Posted - 2008-10-30 : 17:23:37
I have the following stored procedure:

ALTER PROCEDURE [dbo].[training_Results]
@ssn varchar(10), @baid uniqueidentifier

AS

UPDATE EResults SET ba_id = @baid
WHERE test_emp_id = right(@ssn, 6)

I have another table which holds all of my employees. I only want to update the EResults if there is only 1 employee in the Employee table with those last 6 ssn digits. If there is more than one employee with that ssn, then I do not want to update the EResults. How would I do that?

Thanks!

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-30 : 17:30:57
UPDATE EResults SET ba_id = @baid
from EResults r inner join Employee e
on r.test_emp_id = e.emp_id
where r.test_emp_id = right(@ssn,6)
having count(e.emp_id) = 1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-31 : 00:09:21
[code]UPDATE r
SET r.ba_id = @baid
FROM EResults r
INNER JOIN (SELECT COUNT(emp_id) AS EmpCount
FROM Employee e
WHERE emp_id=r.test_emp_id
AND emp_id = right(@ssn,6))e
WHERE e.EmpCount = 1[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-31 : 00:09:54
quote:
Originally posted by hanbingl

UPDATE EResults SET ba_id = @baid
from EResults r inner join Employee e
on r.test_emp_id = e.emp_id
where r.test_emp_id = right(@ssn,6)
having count(e.emp_id) = 1


wont work. you're taking count without grouping by anything.
Go to Top of Page

lsulindy
Starting Member

9 Posts

Posted - 2008-10-31 : 15:19:51
quote:
Originally posted by visakh16

UPDATE r 
SET r.ba_id = @baid
FROM EResults r
INNER JOIN (SELECT COUNT(emp_id) AS EmpCount
FROM Employee e
WHERE emp_id=r.test_emp_id
AND emp_id = right(@ssn,6))e
WHERE e.EmpCount = 1




Thanks! I get the error "Incorrect syntax near the keyword WHERE" on the last line. Do I need and "ON" for the join? Here's a bit of background. We are transmitted test results for our employees. The only identifier we are sent is the last 6 digits of the employee's ssn. The ba_id is our unique employee number. When the case comes up that 2 of our employees have the same last 6 ssn digits, I do not assign a ba_id to the test result. Someone assigns those manually when that case comes up. So, if more than 1 employee exists in the Employee table with the same 6 digit ssn ending, then I do not want to assign a ba_id to the EResult record. This sp is used as part of a "fix" employee records button. It finds all of the EResults with the same last 6 digits as the employee's ssn and assigns them to the employee's ba_id. If there is more than one employee in our system with this employee's last 6 ssn digits, I do not want to assign all of the EResults to this employee's ba_id b/c then he/she will acquire all of the other employee's test results.

UPDATE r
SET r.ba_id = @ba_id
FROM EResults AS r
INNER JOIN (SELECT COUNT(right(ssn,6)) AS EmpCount
FROM Employee e
WHERE right(ssn,6) = right(@ssn,6)) as c
WHERE (c.EmpCount = 1 AND r.emp_test_id = right(@ssn,6))
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2008-10-31 : 15:58:35
[code]
UPDATE a
SET a.ba_id = @baid
from
EResults a
WHERE
a.test_emp_id = right(@ssn, 6)
and 1 = (Select Count(*) from Employee e where right(e.ssn,6) = right(@ssn,6))
[/code]


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

lsulindy
Starting Member

9 Posts

Posted - 2008-10-31 : 16:21:37
quote:
Originally posted by Vinnie881


UPDATE a
SET a.ba_id = @baid
from
EResults a
WHERE
a.test_emp_id = right(@ssn, 6)
and 1 = (Select Count(*) from Employee e where right(e.ssn,6) = right(@ssn,6))



Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881




THANKS!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-01 : 02:22:22
quote:
Originally posted by lsulindy

quote:
Originally posted by visakh16

UPDATE r 
SET r.ba_id = @baid
FROM EResults r
CROSS APPLY INNER JOIN (SELECT COUNT(emp_id) AS EmpCount
FROM Employee e
WHERE emp_id=r.test_emp_id
AND emp_id = right(@ssn,6))e
WHERE e.EmpCount = 1




Thanks! I get the error "Incorrect syntax near the keyword WHERE" on the last line. Do I need and "ON" for the join? Here's a bit of background. We are transmitted test results for our employees. The only identifier we are sent is the last 6 digits of the employee's ssn. The ba_id is our unique employee number. When the case comes up that 2 of our employees have the same last 6 ssn digits, I do not assign a ba_id to the test result. Someone assigns those manually when that case comes up. So, if more than 1 employee exists in the Employee table with the same 6 digit ssn ending, then I do not want to assign a ba_id to the EResult record. This sp is used as part of a "fix" employee records button. It finds all of the EResults with the same last 6 digits as the employee's ssn and assigns them to the employee's ba_id. If there is more than one employee in our system with this employee's last 6 ssn digits, I do not want to assign all of the EResults to this employee's ba_id b/c then he/she will acquire all of the other employee's test results.

UPDATE r
SET r.ba_id = @ba_id
FROM EResults AS r
INNER JOIN (SELECT COUNT(right(ssn,6)) AS EmpCount
FROM Employee e
WHERE right(ssn,6) = right(@ssn,6)) as c
WHERE (c.EmpCount = 1 AND r.emp_test_id = right(@ssn,6))


sorry it should be CROSS APPLY
Go to Top of Page

lsulindy
Starting Member

9 Posts

Posted - 2008-11-13 : 15:29:12
quote:
Originally posted by Vinnie881


UPDATE a
SET a.ba_id = @baid
from
EResults a
WHERE
a.test_emp_id = right(@ssn, 6)
and 1 = (Select Count(*) from Employee e where right(e.ssn,6) = right(@ssn,6))



Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881



That works great. Now I want it to count across two tables. I have an Applicant table and an Employee table. They both have ssn fields. They are not joined in any way. One is a table of applicants, one of hired employees. I want the count of applicants + employees where right(ssn,6) = right(@ssn,6)
Go to Top of Page
   

- Advertisement -