4

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

产品

_____________________________
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

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

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

4 回答 4

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

综合结果

(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 小提琴

于 2013-06-08T17:04:48.737 回答
2

哈克,但它有效

(
  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

于 2013-06-08T17:03:45.413 回答
0

因为我收到一个Cannot perform an aggregate function on an expression containing an aggregate or a subquery as an error错误。

对于其他遇到在SUM函数内部使用MAX函数但与 OP 没有相同问题的问题的人,根据Luv的回答,我能够转换以下内容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

我认为这也可能对其他人有所帮助

于 2018-10-04T15:08:45.783 回答
-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)
于 2013-06-08T16:47:40.247 回答