views:

69

answers:

2

I have a problem to get the max and the min value, I want the result to be XXL and XXS

  SELECT MAX(tblSizeXL.SizeXLName) AS maxSize, 
         MIN(tblSizeXL.SizeXLName) AS minSize    
    FROM Product 
    JOIN tblSizeXL ON Product.SizeXLID = tblSizeXL.SizeXLID
   WHERE (Product.GroupID = @GroupID)
GROUP BY tblSizeXL.SizeXLID
ORDER BY tblSizeXL.SizeXLID DESC

tblSize

SizeXLID     SizeXLName
-----------------------
1            XXS
2            XS
3            S
4            M
5            L
6            XL
7            XXL
8            XXXL
A: 

Remove the group by clause and your query should be fine.

TheCee
Then I get this: Column "tblSizeXL.SizeXLID" is invalid in the ORDER Y clause because it is not contained in either an aggregate function or the GROUP BY clause.
Nicklas
If I understood your question, you expect the result to be a single row. In that case you don't need the 'order by' clause
TheCee
+1  A: 

You're going to have to do an inner queries to get the data you're looking for:

SELECT max.SizeXLName as maxSize, min.SizeXLName as minSize
  FROM 
  (SELECT MAX(tblSizeXL.SizeXLID) as MaxSizeXLID, MIN(tblSizeXL.SizeXLID) as MinSizeXLID
     FROM Product
     JOIN tblSizeXL ON Product.SizeXLID = tblSizeXL.SizeXLID
    WHERE Product..GroupID = @GroupID) base
  JOIN tblSizeXL max ON max.SizeXLID = base.MaxSizeXLID
  JOIN tblSizeXL min on min.SizeXLID = base.MinSizeXLID
Jeff Wight
Looks better, but the result is wrong, I get XXXL and XS, so its almost :)
Nicklas
@Nicklas, are you sure that XXS exists in your Product table for the @GroupID you selected?
Mark Bannister
My fault, there is an XXS but there was no Product that had that size. Great thanks alot!
Nicklas