Click here to Skip to main content
11,410,536 members (55,441 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server
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?
Posted 1-Nov-12 0:24am
Comments
Abhinav S at 1-Nov-12 5:41am
   
UNION removes distinct rows by default.
Are you sure your data has duplicate rows?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

UNION removes distinct rows by default.
Check your joins to ensure no duplicates are being returned.
  Permalink  
Comments
Rkchowdary111 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,755
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web02 | 2.8.150414.5 | Last Updated 1 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100