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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Flag on Create SQL?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

2 Posts

Posted - 11/19/2012 :  19:01:18  Show Profile  Reply with Quote
There are two tables, AccountsOpen and AccountsClosed, which keeps track of which employee has openedand closedeach account
EmployeeID (int)
AccountID (int)

EmployeeID (int)
AccountD (int)

If an employee closes an account they get 1 point
If no one closes an account, the employee that opens the account gets 1 point

Flowing Fount of Yak Knowledge

7174 Posts

Posted - 11/19/2012 :  20:06:25  Show Profile  Reply with Quote
so What is the question?
Go to Top of Page

Starting Member

2 Posts

Posted - 11/19/2012 :  22:53:29  Show Profile  Reply with Quote
How do I set this up?
Go to Top of Page

Flowing Fount of Yak Knowledge

2242 Posts

Posted - 11/20/2012 :  02:22:13  Show Profile  Reply with Quote

declare @AccountsOpen table(EmployeeID int, AccountID int)

declare @AccountsClosed table(EmployeeID int, AccountID int)

insert into @AccountsOpen values(1, 101),(2,201),(3,301),(4,401)

insert into @AccountsClosed values(1, 101),(1,201),(3,401)

Here Two accounts(101,201) colsed by  employee1
     One account(401) is closed by employee3
     301 is not yet closed so employee3 gets another point
    As per my understanding, result should be as follows:
    employeeid    TotalPoints
    1		      2
    3		      2
SELECT employeeid, count(points) 'totalPoints'
FROM (SELECT employeeid, 1 points
		FROM @AccountsOpen
		WHERE AccountID NOT IN (select AccountID from @AccountsClosed)

		UNION all
		select EmployeeID, 1 Points 
		from @AccountsClosed 

	  ) t
GROUP BY employeeid	

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000