'How to select distinct value
I need to select all supervisor records. Our HR employee table set up like below
| Firstname | Lastname | Supervisor | |
|---|---|---|---|
| Frank | Johns | fjohns | Taylor, Don |
| Pat | Hope | phope | Taylor, Don |
| Jen | Dow | jdow | Taylor, Don |
| Taylor | Don | tdon | Olson, Mike |
| Kim | Ronda | kronda | Olson, Mike |
| Rob | Smith | rsmith | Olson, Mike |
| Mike | Olson | molson | null |
The final result should be like this
| Firstname | Lastname | Supervisor | |
|---|---|---|---|
| Taylor | Don | tdon | Olson, Mike |
| Mike | Olson | molson | null |
If I use Distinct on supervisor then it gives me a list of supervisor but not their info. Please help
Solution 1:[1]
This should work (I assume the transposed First / Last for Don Taylor is a typo).
SELECT DISTINCT d.Firstname, d.Lastname, d.Email, d.Supervisor
FROM MyTable d
INNER JOIN MyTable sup ON sup.Supervisor = d.Lastname + ', ' + d.Firstname;
or
SELECT Firstname, Lastname, Email, Supervisor
FROM MyTable
WHERE Lastname + ', ' + Firstname IN (
SELECT Supervisor
FROM MyTable
WHERE Supervisor IS NOT NULL
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | squillman |
