获取mysql上的最大和最小和值

时间:2013-06-08 16:37:46

标签: mysql

美好的一天每个人我想要一个可以给我最大和最小总和值的查询。具体来说,我有以下表格:

产品

_____________________________
productID | categoryID| name|
__________|___________|_____|
1          1           "name1"       
2          1           "name2"     
3          1           "name3"    
4          2           "name4"        
5          2           "name5"     
6          1           "name6"        
7          2           "name7"           
8          2           "name8"       

购买

_____________________________
purchaseID | productID| quantity|
___________|___________|_________|
1          1           12     
2          2           13     
3          4           55    
4          4           66       
5          5           99     
6          6           99      
7          5           88           
8          7           12

所以基本上我必须展示最买的产品和最少买的产品.. T试过这个:

SELECT pr.name, max(sum(p2.quantity))
FROM  purchase as p2, product as pr
WHERE p2.IDproduct=pr.IDproduct 
Group by p2.IDproduct desc

但我收到错误代码1111:无效使用组功能。

4 个答案:

答案 0 :(得分:4)

对于最大产品

select t.name,max(t.sum1) MaxProduct
FROM
(SELECT a.name, sum(b.quantity) sum1
FROM  PRODUCT a
INNER JOIN PURCHASES b
ON a.productID = b.productID
GROUP BY a.productID  )t
group by t.name order by MaxProduct desc limit 1

FOR COMBINE RESULT

(select t.name,max(t.sum1) MaxProduct
FROM
(SELECT a.name, sum(b.quantity) sum1
FROM  PRODUCT a
INNER JOIN PURCHASES b
ON a.productID = b.productID
GROUP BY a.productID  )t
group by t.name order by MaxProduct desc limit 1)
UNION ALL
(select t1.name,min(t1.sum1) MaxProduct
FROM
(SELECT a.name, sum(b.quantity) sum1
FROM  PRODUCT a
INNER JOIN PURCHASES b
ON a.productID = b.productID
GROUP BY a.productID  )t1
group by t1.name order by MaxProduct asc limit 1)

<强> SQL FIDDLE

答案 1 :(得分:2)

Hacky,但它有效

(
  SELECT 
    SUM(pur.quantity) quant, 
    prod.name name
  FROM Purchases pur
  INNER JOIN Products prod
    ON prod.productID = pur.productID
  GROUP BY pur.productID
  ORDER BY quant DESC
  LIMIT 1
)
UNION
(
  SELECT 
    SUM(pur.quantity) quant, 
    prod.name name
  FROM Purchases pur
  INNER JOIN Products prod
    ON prod.productID = pur.productID
  GROUP BY pur.productID
  ORDER BY quant ASC
  LIMIT 1
)

SQLFiddle

答案 2 :(得分:0)

当我收到Cannot perform an aggregate function on an expression containing an aggregate or a subquery as an error错误时。

基于 Luv 的回答,对于其他遇到在SUM函数内部使用MAX函数但与OP没有相同问题的用户,我能够转换以下Scalar-valued function

SELECT @MinMaxValue = MAX(SUM(ArtikelM.Omzet)/SUM(ArtikelM.Aantal))
FROM ArtikelM
    INNER JOIN Klanten
        ON ArtikelM.KlantenId = Klanten.KlantenId
    WHERE ArtikelId = @ArtikelId
        AND Klanten.KlantTypeCategorie = @KlantType
        AND Klanten.KlantGrootteCategorie = @KlantGrootte
        AND ArtikelM.Jaar = @Jaar

SELECT @MinMaxValue = MAX(selectedsum.sumx)
FROM (
    SELECT SUM(ArtikelM.Omzet)/SUM(ArtikelM.Aantal) AS sumx
    FROM ArtikelM
    INNER JOIN Klanten
        ON ArtikelM.KlantenId = Klanten.KlantenId
    WHERE ArtikelId = @ArtikelId
        AND Klanten.KlantTypeCategorie = @KlantType
        AND Klanten.KlantGrootteCategorie = @KlantGrootte
        AND ArtikelM.Jaar = @Jaar
) AS selectedsum

或者ArtikelM.Aantal是否可以为零

SELECT sumx =
    CASE
        WHEN SUM(ArtikelM.Aantal) > 0
        THEN SUM(ArtikelM.Omzet)/SUM(ArtikelM.Aantal)
        ELSE MIN(Leverancier.FactPrijs)
    END
FROM ArtikelM
INNER JOIN Leverancier
    ON ArtikelM.LevId = Leverancier.LevId
INNER JOIN Klanten
    ON ArtikelM.KlantenId = Klanten.KlantenId
WHERE ArtikelId = @ArtikelId
    AND Klanten.KlantTypeCategorie = @KlantType
    AND Klanten.KlantGrootteCategorie = @KlantGrootte
    AND ArtikelM.Jaar = @Jaar
) AS selectedsum

我认为这也可能会帮助其他人

答案 3 :(得分:-1)

Select max(product_id), min(product_id) 
from Purchases Where Product_id In
(select product_id, Sum(quantity) from Purchases Group by product_id)