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 varcharTable EmployeeActivityid fname lname Event1 Jane Jones Active2 Ed Smith Active3 Mary Jones Active1 Jane Jones Not Active4 Bob Jones Active2 Ed Smith Not Active5 Alice White Not ActiveThe 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 CNTfrom Test Ajoin 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 tINNER JOIN (SELECT lname FROM Table GROUP BY lname HAVING COUNT(DISTINCT fname)=1 )t1ON t1.lname = t.lnameWHERE t.Event = 'Active'[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
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 operatorThe fields are all varchar(50) so am not sure what the text, ntext, and image data types mean.(thanks again in advance!) |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
|
|
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 )bWHERE b = 1[/code]cant sleep and brain started to crash |
|
|
SandyBon
Starting Member
4 Posts |
Posted - 2013-03-01 : 14:29:54
|
Hi waterduck - there has to be a self join - but thank you! |
|
|
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) |
|
|
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 errorthe text, ntext and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|