Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting Duplicate rows in my query, despite using UNION and DISTINCT keywords.

Here is my query:
SQL
(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?
Posted
Comments
Abhinav S 1-Nov-12 5:41am    
UNION removes distinct rows by default.
Are you sure your data has duplicate rows?

1 solution

UNION removes distinct rows by default.
Check your joins to ensure no duplicates are being returned.
 
Share this answer
 
Comments
.net333 1-Nov-12 6:16am    
here Admin data is in two tables

1) Users 2) Employees Tables

first query is for fetching the UsersAppointments

second Query is for fetching the Employees Appointments..So here Same Data is Repeating for Admins

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900