Click here to Skip to main content
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 OriginalGriff 274
1 Shweta N Mishra 216
2 PIEBALDconsult 210
3 Sergey Alexandrovich Kryukov 185
4 BillWoodruff 174
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 1 Nov 2012
Copyright © CodeProject, 1999-2014
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