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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Select Distinct multiple columns, but not all

Author  Topic 

cortpcl
Starting Member

4 Posts

Posted - 2014-06-30 : 10:44:23
Hi,

I need some expert help.

I am doing a select distinct on multiple columns.
But there are some columns, i would like to exclude from this distinct selection, cause the should be different.

Can that be done at all?
In the following select i would like to exclude; PartnerId, PartnerName and DISTANCE from the distinct.


SELECT
Distinct P.TyreTreadPattern
,CONVERT(INT,REPLACE(P.ListPrice,',000','')) as ListPrice
,dbo.fnPercent(U.PercentFactor,P.ListPrice) AS NORMALPRICE
,CASE
WHEN NOT C.PercentFactor IS NULL THEN dbo.fnPercent(C.PercentFactor,dbo.fnPercent(U.PercentFactor,P.ListPrice))
ELSE 0
END AS REDUCEDPRICE
,CASE
WHEN NOT C.PercentFactor IS NULL THEN convert(int,dbo.fnPercent(U.PercentFactor,P.ListPrice)) - convert(int, dbo.fnPercent(C.PercentFactor,dbo.fnPercent(U.PercentFactor,P.ListPrice)))
ELSE 0
END AS SAVEAMOUNT
,CASE
WHEN NOT C.PercentFactor IS NULL THEN U.PercentFactor+C.PercentFactor
ELSE U.PercentFactor
END AS FULLPERCENT
,ISNULL(C.PercentFactor,0) AS FULLPERCENTFACTOR
,U.PercentFactor AS NORMALPROCENT
,U.UndergruppeId
,U.UndergruppeNavn
,P.TyreSectionWidth
,P.TyreAspectRatio
,P.TyreRimDiameter
,P.TyreSpeedRating
,P.TyreLoadIndex
,P.TyreWet
,P.TyreType
,P.TyreNoise
,P.TyreNoiseClass
,P.TyreFuel
,C.PartnerId
,dbo.GetUMBMemberPropertyValue(C.PartnerId, 'memWorkshopName') AS PartnerName
, dbo.Distance(dbo.GetUMBMemberPropertyValue(C.PartnerId, 'latitude'),dbo.GetUMBMemberPropertyValue(C.PartnerId, 'longitude'),@lat,@lng) AS DISTANCE

FROM [cartendo_dk_db].[dbo].[CompilatorPricemanager] AS P

INNER JOIN CompilatorPricemanagerUnderGrupper AS U ON U.UndergruppeId = P.SubGroup

LEFT JOIN dbo.Partner_Campaigns as C ON
(
(U.UndergruppeId = C.TyreId )
OR
(C.CarId = (SELECT id FROM Partner_CarModels WHERE Make like '%' + @CarName + '%') )
OR
(C.RegionsNr = isnull((SELECT DISTINCT(Regionsnr) FROM Partner_Regioner_Kommuner_Postnr WHERE Postnr=@Zipcode), C.RegionsNr) )
OR
(
C.CarAgeGroup =
isnull((
CASE
WHEN @CarAge < 6 THEN 1
ELSE 0
END
), C.CarAgeGroup)
)
)

WHERE
P.MainGroup= @TyreType
AND
p.ListPrice <> '0,000'
AND
P.[TyreSectionWidth] = @SectionWidth
AND
P.[TyreAspectRatio]=@AspectRatio
AND
P.[TyreRimDiameter]=@Diameter

ORDER BY
REDUCEDPRICE ASC,
DISTANCE ASC

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-06-30 : 11:02:20
If there are two rows with all columns having the same value except for, for example, PartnerName being different, which of these two rows would you like to pick? In any case, one way to do this type of query is to use the row_number function. For example the following code will pick will one row per each unique combination of colA, colB (the partition by clause does this) and if there is more than one row with a given value of colA and colB, it will pick the one with the lowest value of colId
;WITH cte AS
(
SELECT colA, colB, colC, colD,
ROW_NUMBER() OVER (PARTITION BY colA,colB ORDER BY colD) AS RN
FROM
YourTable
)
SELECT colA, colB, colC, ColD
FROM cte WHERE RN = 1;
Go to Top of Page
   

- Advertisement -