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

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

regex
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
AccountsOpen
EmployeeID (int)
AccountID (int)

AccountsClosed
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

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

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

regex
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

bandi
Flowing Fount of Yak Knowledge

India
2202 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	


--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.05 seconds. Powered By: Snitz Forums 2000