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
 General SQL Server Forums
 New to SQL Server Programming
 Join with a partiion over function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SandyBon
Starting Member

4 Posts

Posted - 03/01/2013 :  11:29:28  Show Profile  Reply with Quote
Hello,

I recently had a tech interview/test and had to come up with the following.

Display only once, each employee from an employee activity table. (they could be in ther multiple times for activities) All employees that do not have the same last name as another employee, (even with different fname) they must have Yes in the Activity field. MUST use a self table join.

Data all varchar
Table EmployeeActivity

id fname lname Event
1 Jane Jones Active
2 Ed Smith Active
3 Mary Jones Active
1 Jane Jones Not Active
4 Bob Jones Active
2 Ed Smith Not Active
5 Alice White Not Active

The only output should be Ed Smith.

I didn't get the job, and knew I wouldn't as it was for a more senior programmer, but wanted to try out anyway. But would like to understand the syntax of using a self join in this situation.

Here is my query ( I made it work without a join, but I am not sure how to do it with a SELF JOIN )

select A.Fname, A.Lname, A.Active, COUNT(A.Fname)OVER(PARTITION BY A.LNAME) as CNT
from Test A
join Test B on A.Lname = B.Lname where B.Lname NOT IN (select Lname from A where CNT = 1 and A.activity = 'Active'
Group by A.Lname, A.Fname, A.Activity

Thanks hope this is clear, or at least you can see what I am doing and it is simple but does not work.

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 03/01/2013 :  12:41:36  Show Profile  Reply with Quote

SELECT t.* 
FROM Table t
INNER JOIN (SELECT lname
            FROM Table
            GROUP BY lname
            HAVING COUNT(DISTINCT fname)=1
           )t1
ON t1.lname = t.lname
WHERE t.Event = 'Active'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

SandyBon
Starting Member

4 Posts

Posted - 03/01/2013 :  13:02:05  Show Profile  Reply with Quote
Thank you visakh16 this looks more like what I should do, but I do get an error message:

the text, ntext and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator

The fields are all varchar(50) so am not sure what the text, ntext, and image data types mean.

(thanks again in advance!)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 03/01/2013 :  13:09:03  Show Profile  Reply with Quote
i think fname,lname is of text or ntext type. check the datatype.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

waterduck
Aged Yak Warrior

Malaysia
942 Posts

Posted - 03/01/2013 :  14:07:01  Show Profile  Reply with Quote
SELECT	*
FROM	(
	SELECT	*
	,	b = SUM(a) OVER (PARTITION BY LNAME)
	FROM	(
		SELECT	*
		,	a = ROW_NUMBER() OVER (PARTITION BY LNAME ORDER BY LNAME)
		FROM	EmployeeActivity
		WHERE	Event = 'Active'
		)a
	)b
WHERE	b = 1

cant sleep and brain started to crash
Go to Top of Page

SandyBon
Starting Member

4 Posts

Posted - 03/01/2013 :  14:29:54  Show Profile  Reply with Quote
Hi waterduck - there has to be a self join - but thank you!
Go to Top of Page

SandyBon
Starting Member

4 Posts

Posted - 03/01/2013 :  14:37:31  Show Profile  Reply with Quote
visakh16 - here are the datatypes on this table (thank you)

Id (varchar(50) not null)
Fname (varchar(50) not null)
Lname (varchar(50) not null)
Event (varchar(50) not null)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 03/02/2013 :  02:18:26  Show Profile  Reply with Quote
not sure thats correct, as in that case you wont get the below error

the text, ntext and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.08 seconds. Powered By: Snitz Forums 2000