SELECT DISTINCT et COMMANDE PAR

Si je place mot clé DISTINCT, j'obtiens une erreur d'autres sages, il fonctionne très bien.

ERREUR: Msg 145, Niveau 15, État 1, Procédure SP_Products_GetList, la Ligne 15
COMMANDE PAR éléments doivent apparaître dans la liste de sélection si SELECT DISTINCT est spécifié.

ALTER PROCEDURE [dbo].[SP_Products_GetList]    
@CatID int,
@CatName int,
@IsNew bit,
@InActive bit,
@SortBy varchar(50),
@SortType varchar(50)
AS    
SELECT DISTINCT Products.ProductID, ProductName, MAX(Price) Price, PriceID, [Description], Size, IsNew, InActive FROM (SELECT * FROM Products WHERE (@InActive is null or @InActive = InActive  ) AND ( @IsNew is null or @IsNew = IsNew )) Products
INNER JOIN  ProductCategory
on Products.ProductID = ProductCategory.ProductID 
INNER JOIN  (
SELECT * FROM Categories 
WHERE 
( @CatID is null or @CatID = CatID ) and
( @CatName is null or @CatName = CatName )
) Categories 
on ProductCategory.CatID = Categories.CatID 
INNER JOIN ( 
SELECT Prices.ProductID, Prices.Price, Prices.PriceID, Prices.SizeID  FROM Prices 
INNER JOIN (
SELECT ProductID, max(Price) Price from Prices WHERE PriceID IN 
( SELECT MAX(PriceID) FROM Prices 
GROUP BY ProductID , SizeID)
GROUP BY ProductID )  Prices_
ON Prices.ProductID = Prices_.ProductID AND Prices.Price = Prices_.Price                     
) as Prices 
on Prices.ProductID = Products.ProductID 
inner join  Sizes 
on Sizes.SizeID = Prices.SizeID 
GROUP BY ProductName, CatName, Products.ProductID, Price, PriceID, [Description] ,Size, IsNew,InActive 
ORDER BY 
CASE @SortType 
WHEN 'desc' THEN  
CASE @SortBy         
WHEN 'ProductName' THEN ProductName        
END  
END 
DESC, 
CASE @SortType 
WHEN 'desc' THEN  
CASE @SortBy         
WHEN 'ProductID' THEN Products.ProductID
WHEN 'Price' THEN Price          
END  
END 
DESC,     
CASE @SortType 
WHEN 'asc' THEN  
CASE @SortBy         
WHEN 'ProductName' THEN ProductName        
END  
END 
ASC, 
CASE @SortType 
WHEN 'asc' THEN  
CASE @SortBy         
WHEN 'ProductID' THEN Products.ProductID
WHEN 'Price' THEN Price          
END  
END 
ASC
Qui veut trouver l'aiguille?
Juste un conseil: essayez d'éviter d'utiliser SELECT * pour des raisons de performances...
Thnx pour la suggestion

OriginalL'auteur SOF User | 2010-08-09