Solved

SQL Distinct

Posted on 2014-01-23
2
646 Views
Last Modified: 2014-01-23
I'm getting duplicate records running this query and would like to eliminate the client name duplicates.

Was trying to do a distinct but still results had duplicates.
----------------------------------------------------------------------------------------------------------------------------
SELECT    DISTINCT Clients.ClientCode, Clients.LastName, Clients.FirstName, Clients.DateOfBirth, Clients.Disability, Clients.PrefSpaceType, Clients.EligFromDate, Clients.EligToDate,  ClientStats.TripCount

FROM         Clients INNER JOIN
                      ClientStats AS ClientStats ON Clients.ClientId = ClientStats.ClientId
WHERE     (Clients.InActive = 0) AND (Clients.PrefSpaceType = 'SC' OR
                      Clients.PrefSpaceType = 'ST' OR
                      Clients.PrefSpaceType = 'XL' OR
                      Clients.PrefSpaceType = 'XW')
ORDER BY Clients.LastName
0
Comment
Question by:bjbrown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39804302
Why don't you use group by clause if possible
0
 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 39804346
You should aggregate the trip count:

SELECT  Clients.ClientCode, Clients.LastName, Clients.FirstName, Clients.DateOfBirth, Clients.Disability, Clients.PrefSpaceType, Clients.EligFromDate, Clients.EligToDate,  SUM(ClientStats.TripCount) AS TripCount
FROM         Clients INNER JOIN
                      ClientStats AS ClientStats ON Clients.ClientId = ClientStats.ClientId
WHERE     (Clients.InActive = 0) AND (Clients.PrefSpaceType = 'SC' OR
                      Clients.PrefSpaceType = 'ST' OR
                      Clients.PrefSpaceType = 'XL' OR
                      Clients.PrefSpaceType = 'XW')
GROUP BY Clients.ClientCode, Clients.LastName, Clients.FirstName, Clients.DateOfBirth, Clients.Disability, Clients.PrefSpaceType, Clients.EligFromDate, Clients.EligToDate
ORDER BY Clients.LastName
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question