I am getting Duplicate rows in my query, despite using UNION and DISTINCT keywords.
Here is my query:
(SELECT DISTINCT A.AppointmentId,
u.Firstname,
u.Gender,
u.UserEmail,
u.PhoneNo,
a.PatientFirstName,
a.PatientLastName,
a.PatientGender,
a.Date,
a.TimeStart,
a.CompanyId,
C.CompanyName,
a.ProviderId,
a.TimeEnd,
a.Reason,
Ad.Line1,
Ad.Line2,
Ad.City,
Ad.State,
Ad.Zip
FROM Users u,
Appointment a,
Company C,
Address Ad
WHERE u.UserId = a.UserId
AND A.CompanyId = C.Id
AND C.AddressId = Ad.Id
AND A.ProviderId = '2EA0430F-3775-4623-92B1-5045CF836C11')
UNION
(SELECT DISTINCT A.AppointmentId,
E.Name AS FirstName,
E.Gender,
E.Email AS UserEmail,
E.PhoneNumber AS PhoneNo,
a.PatientFirstName,
a.PatientLastName,
a.PatientGender,
A.Date,
A.TimeStart,
A.CompanyId,
C.CompanyName,
A.ProviderId,
A.TimeEnd,
A.Reason,
Ad.Line1,
Ad.Line2,
Ad.City,
Ad.State,
Ad.Zip
FROM Appointment A,
Employee E,
Company C,
Address Ad
WHERE A.ProviderId = '2EA0430F-3775-4623-92B1-5045CF836C11'
AND C.AddressId = Ad.Id
AND A.UserId = E.EmployeeId
AND A.CompanyId = C.Id)
ORDER BY A.Date DESC
Can anybody tell me why this is happening, and help me fix it?