I have an issue where I am looking for missing records. I can set up queries with Left Joins and look for nulls to tell if records are missing but Left Joins will also give you multiple records for parent. I only want to know if any record exists or not. The actual records are irrelevant here.
I just want to see if any record (showing the ID - any ID) or a null. If it has 3 records I don't want to see 3 records, I want to see only one and display one of the keys (doesn't matter which one). If there is a null, a message gets printed.
Here is a sample of what I am trying to do:
Result:
What this tell me is:
Employee 1 has a name record and a phone.
Employee 2 has a name record and 3 phones.
Employee 3 had no name records and as such has no phones
Employee 4 has a name record but no phones.
What I want to see is:
I don't care that employee 2 has 3 phones. I only want to know that the employee has at least one phone record.
How can I change my query to show this? I had thought of using "Exists" but that filter out the employee. I still want to see the employee.
Thanks,
Tom
I just want to see if any record (showing the ID - any ID) or a null. If it has 3 records I don't want to see 3 records, I want to see only one and display one of the keys (doesn't matter which one). If there is a null, a message gets printed.
Here is a sample of what I am trying to do:
Code:
If OBJECT_ID('tempdb..#Employee') IS NOT NULL
DROP TABLE #Employee
If OBJECT_ID('tempdb..#Name') IS NOT NULL
DROP TABLE #Name
If OBJECT_ID('tempdb..#Phone') IS NOT NULL
DROP TABLE #Phone
CREATE TABLE #Employee
(
EmployeeID int
)
CREATE TABLE #Name
(
NameID int,
EmployeeID int,
Name varchar(100)
)
CREATE TABLE #Phone
(
PhoneID int,
NameID int,
PhoneNumber varchar(100),
PhoneType varchar(20)
)
INSERT #Employee (EmployeeID) Values (1)
INSERT #Employee (EmployeeID) Values (2)
INSERT #Employee (EmployeeID) Values (3)
INSERT #Employee (EmployeeID) Values (4)
INSERT #Name (NameID, EmployeeID, Name) Values (1,1,'Tom')
INSERT #Name (NameID, EmployeeID, Name) Values (2,2,'Mary')
INSERT #Name (NameID, EmployeeID, Name) Values (3,4,'Joe')
INSERT #Phone (PhoneID, NameID, PhoneNumber, PhoneType) Values (1,1,'2129991234','Home')
INSERT #Phone (PhoneID, NameID, PhoneNumber, PhoneType) Values (2,2,'4159991111','Home')
INSERT #Phone (PhoneID, NameID, PhoneNumber, PhoneType) Values (3,2,'4158112222','Business')
INSERT #Phone (PhoneID, NameID, PhoneNumber, PhoneType) Values (4,2,'7149998888','Mobile')
SELECT e.EmployeeID, n.NameID, p.PhoneID
FROM #Employee e
LEFT JOIN #Name n
ON e.EmployeeID = n.EmployeeID
LEFT JOIN #Phone p
ON n.NameID = p.NameID
Result:
Code:
EmployeeID NameID PhoneID
1 1 1
2 2 2
2 2 3
2 2 4
3 NULL NULL
4 3 NULL
What this tell me is:
Employee 1 has a name record and a phone.
Employee 2 has a name record and 3 phones.
Employee 3 had no name records and as such has no phones
Employee 4 has a name record but no phones.
What I want to see is:
Code:
EmployeeID NameID PhoneID
1 1 1
2 2 2
3 NULL NULL
4 3 NULL
I don't care that employee 2 has 3 phones. I only want to know that the employee has at least one phone record.
How can I change my query to show this? I had thought of using "Exists" but that filter out the employee. I still want to see the employee.
Thanks,
Tom