SQL - 显示均价的最小值
SQL - Display the minimum value of an average price
我的数据库包含产品和成品列表。我已经能够成功地平均每个完成选项的价格,但我只想显示价格最低的选项。
这是我的数据样本。
Table 1 - product_t
ProductID ProductLineID ProductDescription ProductFinish ProductStandardPrice ProductOnHand
1 1 "Cherry End Table" Cherry 175.00 0
2 1 "Birch Coffee Tables" Birch 200.00 0
3 1 "Oak Computer Desk" Oak 750.00 0
4 1 "Entertainment Center" Cherry 1650.00 0
此查询会生成一个列表,其中包含每个可用的产品饰面以及具有该饰面的商品的平均价格。
SELECT ProductFinish, AVG(ProductStandardPrice) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
但是,我只想显示此查询产生的最低价格。我尝试了这个查询,但它不会执行。
SELECT ProductFinish, MIN (AVG(ProductStandardPrice)) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
在此先感谢您的帮助。非常感谢。
获得最低值的典型方法是使用order by
和某种对行数的限制。在 ANSI 标准 SQL 中,这将是:
SELECT ProductFinish, AVG(ProductStandardPrice) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
ORDER BY AveragePrice ASC
FETCH FIRST 1 ROW ONLY;
请注意,某些数据库使用 LIMIT 1
或 TOP 1
而不是 FETCH FIRST 1 ROW ONLY
。此外,此版本仅获取价格最低的一条记录。如果存在重复的最小值,则选择任意值。
我的数据库包含产品和成品列表。我已经能够成功地平均每个完成选项的价格,但我只想显示价格最低的选项。
这是我的数据样本。
Table 1 - product_t
ProductID ProductLineID ProductDescription ProductFinish ProductStandardPrice ProductOnHand
1 1 "Cherry End Table" Cherry 175.00 0
2 1 "Birch Coffee Tables" Birch 200.00 0
3 1 "Oak Computer Desk" Oak 750.00 0
4 1 "Entertainment Center" Cherry 1650.00 0
此查询会生成一个列表,其中包含每个可用的产品饰面以及具有该饰面的商品的平均价格。
SELECT ProductFinish, AVG(ProductStandardPrice) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
但是,我只想显示此查询产生的最低价格。我尝试了这个查询,但它不会执行。
SELECT ProductFinish, MIN (AVG(ProductStandardPrice)) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
在此先感谢您的帮助。非常感谢。
获得最低值的典型方法是使用order by
和某种对行数的限制。在 ANSI 标准 SQL 中,这将是:
SELECT ProductFinish, AVG(ProductStandardPrice) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
ORDER BY AveragePrice ASC
FETCH FIRST 1 ROW ONLY;
请注意,某些数据库使用 LIMIT 1
或 TOP 1
而不是 FETCH FIRST 1 ROW ONLY
。此外,此版本仅获取价格最低的一条记录。如果存在重复的最小值,则选择任意值。