开发者

SQL MAX and MIN in one column

开发者 https://www.devze.com 2023-01-18 06:16 出处:网络
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,

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消