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
 Join with a partiion over function

Author  Topic 

SandyBon
Starting Member

4 Posts

Posted - 2013-03-01 : 11:29:28
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

52326 Posts

Posted - 2013-03-01 : 12:41:36
[code]
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'
[/code]

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

Go to Top of Page

SandyBon
Starting Member

4 Posts

Posted - 2013-03-01 : 13:02:05
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

52326 Posts

Posted - 2013-03-01 : 13:09:03
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

982 Posts

Posted - 2013-03-01 : 14:07:01
[code]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[/code]
cant sleep and brain started to crash
Go to Top of Page

SandyBon
Starting Member

4 Posts

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

SandyBon
Starting Member

4 Posts

Posted - 2013-03-01 : 14:37:31
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

52326 Posts

Posted - 2013-03-02 : 02:18:26
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
   

- Advertisement -