Hi
Does any one know of a way to achieve the following?
I have a query that returns the results I want, except there are duplicates due to a related table that can have many related records.
I am looking to ensure only one record is returned for any found [tblPump.lPumpID] ordered by [tblPumpMovement.dDateIn]. Apologies in advance if I have not provided enough information so far, for your help...
My query so far is:
SELECT DISTINCT
tblPump.lPumpID, tblPump.sPumpSerialNumber, tblPump.lPumpModelID, tblPump.lClientID, tblPump.bDeactivated, tblPump.bDeleted,
tblPumpMovement.lPumpMovementID, tblSite_1.sSiteName, tblSite_1.lSiteID, tblRegion.sRegion, tblPumpMovement.bPumpOut, tblPumpPoint.lPumpPointID,
tblPumpMovement.dDateOut, tblPumpMovement.dDateIn
FROM tblPumpPoint INNER JOIN
tblSamplePoint ON tblPumpPoint.lSamplePointID = tblSamplePoint.lSamplePointID INNER JOIN
tblSite AS tblSite_1 ON tblSamplePoint.lSiteID = tblSite_1.lSiteID INNER JOIN
tblRegion ON tblSite_1.lRegionID = tblRegion.lRegionID LEFT OUTER JOIN
tblPumpMovement ON tblPumpPoint.lPumpPointID = tblPumpMovement.lPumpPointID RIGHT OUTER JOIN
tblRegion AS tblRegion_1 INNER JOIN
tblSite ON tblRegion_1.lRegionID = tblSite.lRegionID INNER JOIN
tblClient ON tblRegion_1.lClientID = tblClient.lClientID INNER JOIN
tblPump ON tblClient.lClientID = tblPump.lClientID ON tblPumpMovement.lPumpID = tblPump.lPumpID
WHERE (tblSite.lSiteID IN
(SELECT value
FROM dbo.fn_Split(@SiteIDs, ',') AS fn_Split_1)) AND (tblPumpMovement.bPumpOut = 1) OR
(tblSite_1.lSiteID IN
(SELECT value
FROM dbo.fn_Split(@SiteIDs, ',') AS fn_Split_1)) OR
(tblSite_1.lSiteID IS NULL)
ORDER BY tblPumpMovement.dDateIn, tblPumpMovement.dDateOut, tblPump.sPumpSerialNumber