Advertisement

04.05.2008 at 09:16AM PDT, ID: 23298453
[x]
Attachment Details

remove duplicates by max value of three columns

Asked by dgdfgssfdgs in MS SQL Server, SQL Server 2005

i have a join of a "customers" table to three related tables: "sales","Parts","service"
the problem is wanting to remove duplicates based on the maximum date value found in any of the three tables. i'm getting output like this..
KAREN      DION      11 MAIN ST      city      zip           state     2310499             
KAREN      DION      11 MAIN ST      city      zip            state       9741774
so, the person has two account codes.(the last field). the good record is the one with the most current date found any of the three related tables.      
here's my sql so far...

SELECT DISTINCT
                       Customers.FirstName, Customers.LastName, Customers.Street, Customers.city, Customers.zip,   Customers.AccountCode, Customers.State, Customers.Store
FROM         Customers LEFT OUTER JOIN
                      Parts ON Customers.AccountCode = Parts.AccountCode LEFT OUTER JOIN
                      Sales ON Customers.AccountCode = Sales.AccountCode LEFT OUTER JOIN
                      Service ON Customers.AccountCode = Service.AccountCode
   
WHERE     (Customers.Store = N'plai') AND (Sales.Unit_1_Date > '01/01/2001') and (Sales.Store = N'plai') and Customers.LastName = 'dion' OR
                      (Customers.Store = N'plai') AND (Service.Ro_Date_1 > '01/01/2001') and (Service.Store = N'plai')and Customers.LastName = 'dion' OR
                  (Customers.Store = N'plai') AND (Parts.Invoice_1_Date > '01/01/2001')and (Parts.Store = N'plai') and Customers.LastName = 'dion'

ugly huh?

jim


    Start Free Trial
[+][-]04.05.2008 at 09:20AM PDT, ID: 21288625

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.05.2008 at 10:00AM PDT, ID: 21288778

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.05.2008 at 09:11PM PDT, ID: 21290661

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.06.2008 at 11:31PM PDT, ID: 21294678

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, SQL Server 2005
Sign Up Now!
Solution Provided By: mark_wills
Participating Experts: 4
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628