| Author |
Topic  |
|
|
SandyBon
Starting Member
4 Posts |
Posted - 03/01/2013 : 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
India
47157 Posts |
Posted - 03/01/2013 : 12:41:36
|
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/
|
 |
|
|
SandyBon
Starting Member
4 Posts |
Posted - 03/01/2013 : 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!) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47157 Posts |
Posted - 03/01/2013 : 13:09:03
|
i think fname,lname is of text or ntext type. check the datatype.
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
waterduck
Aged Yak Warrior
Malaysia
791 Posts |
Posted - 03/01/2013 : 14:07:01
|
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 |
 |
|
|
SandyBon
Starting Member
4 Posts |
Posted - 03/01/2013 : 14:29:54
|
| Hi waterduck - there has to be a self join - but thank you! |
 |
|
|
SandyBon
Starting Member
4 Posts |
Posted - 03/01/2013 : 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
India
47157 Posts |
Posted - 03/02/2013 : 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/
|
 |
|
| |
Topic  |
|