Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Selecting Distinct Records with Multiple Columns

Author  Topic 

tokyotwin
Starting Member

2 Posts

Posted - 2011-08-02 : 14:49:05
I am trying to find a way to return all distinct Permit Number records from two different tables' filtered by a specific date range. The following query returns multiple permit numbers. I just need to return unique permit numbers for the specified dates with all of the associated other fields. Is there a way to select all records, but make sure that only the Permit Numbers are distinct? Any direction would be greatly appreciated.

SELECT DISTINCT
v.PermitNumber, p.ApprovedDate, v.ProcessStatus, v.PermitTypeDescription, v.ServiceAddress, v.CityStateZip, v.EstimatedImprovementValue, v.OtherPartyName, v.OtherPartyMailingAddress, v.OtherPartyCityStateZip, v.ParcelNumber, v.PermitIssueTo, v.PermitDescription, v.CurrentPrimaryOwner
FROM PermitCheckList AS p INNER JOIN
vwPermit AS v ON p.PermitID = v.PermitID
WHERE (p.ApprovedDate BETWEEN @StartDate AND @EndDate)
ORDER BY 'PermitNumber'

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-08-02 : 15:26:27
When you use DISTINCT keyword, it determine the "distinctness" based on all the columns in the select list. So if there are two rows which have different approveddate (or any other column that are different), both rows will be returned even if the PermitNumber is the same for both of those rows.

If you want to get only one row for a given permitnumber, you have to decide how to pick the other columns, for a given permit number. You could use a max function (see below) or you could use the row_number() function (see further down below):
SELECT  
v.PermitNumber,
max(p.ApprovedDate) AS ApprovedDate,
max(v.ProcessStatus) AS ProcessStatus,
max(v.PermitTypeDescription) AS PermitTypeDescription,
max(v.ServiceAddress) AS ServiceAddress,
max(v.CityStateZip) AS CityStateZip,
max(v.EstimatedImprovementValue) AS EstimatedImprovementValue,
max(v.OtherPartyName) AS OtherPartyName,
max(v.OtherPartyMailingAddress) AS OtherPartyMailingAddress,
max(v.OtherPartyCityStateZip) AS OtherPartyCityStateZip,
max(v.ParcelNumber) AS ParcelNumber,
max(v.PermitIssueTo) AS PermitIssueTo,
max(v.PermitDescription) AS PermitDescription,
max(v.CurrentPrimaryOwner) AS CurrentPrimaryOwner
FROM
PermitCheckList AS p
INNER JOIN vwPermit AS v
ON p.PermitID = v.PermitID
WHERE
(p.ApprovedDate BETWEEN @StartDate AND @EndDate)
GROUP BY
v.PermitNumber
ORDER BY
v.PermitNumber;


Using row_number.
with cte as
(SELECT
DISTINCT
v.PermitNumber,
p.ApprovedDate,
v.ProcessStatus,
v.PermitTypeDescription,
v.ServiceAddress,
v.CityStateZip,
v.EstimatedImprovementValue,
v.OtherPartyName,
v.OtherPartyMailingAddress,
v.OtherPartyCityStateZip,
v.ParcelNumber,
v.PermitIssueTo,
v.PermitDescription,
v.CurrentPrimaryOwner,
ROW_NUMBER() OVER(PARTITION BY v.PermitNumber ORDER BY (SELECT NULL)) AS RN
FROM
PermitCheckList AS p
INNER JOIN vwPermit AS v
ON p.PermitID = v.PermitID
WHERE
(p.ApprovedDate BETWEEN @StartDate AND @EndDate)
)
SELECT * FROM CTE WHERE RN = 1;
ORDER BY
PermitNumber;
The values in other columns will have different logic in each of the methods. You have to decide what the right thing to do is in your situation.
Go to Top of Page

tokyotwin
Starting Member

2 Posts

Posted - 2011-08-03 : 09:20:46
Thank you Sunita, I tried the first option and it worked perfectly! :) I can't tell you how many different Forums I looked through trying to find a solution to what I presumed to be a very simple problem. I sincerely appreciate your quick response and elegant solution...You Rock! :) Matt
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-08-03 : 11:12:06
You are very welcome and thank you for the kind words.
Go to Top of Page
   

- Advertisement -