SQL 平均多列
SQL Average Multiple Columns
你会如何计算多列和多行的平均值?
到目前为止我的代码是:
SELECT b.id, r.id, b.name, r.business_id, (r.service + r.value + r.quality) / 3 AS average
FROM business b
LEFT JOIN rating r ON r.business_id = b.id
我想得到 r.service、r.value 和 r.quality 的平均值并合并 business_id 列。
所以我应该得到:
business_id = 平均值:9.42(举例)
聚合函数 AVG 应该采用聚合表达式,所以这应该有效:
SELECT b.name, r.business_id, AVG((r.service + r.value + r.quality)/3)
FROM business b
LEFT JOIN rating r ON r.business_id = b.id
GROUP BY b.name, r.business_id
LIMIT 0,30
你会如何计算多列和多行的平均值?
到目前为止我的代码是:
SELECT b.id, r.id, b.name, r.business_id, (r.service + r.value + r.quality) / 3 AS average
FROM business b
LEFT JOIN rating r ON r.business_id = b.id
我想得到 r.service、r.value 和 r.quality 的平均值并合并 business_id 列。
所以我应该得到: business_id = 平均值:9.42(举例)
聚合函数 AVG 应该采用聚合表达式,所以这应该有效:
SELECT b.name, r.business_id, AVG((r.service + r.value + r.quality)/3)
FROM business b
LEFT JOIN rating r ON r.business_id = b.id
GROUP BY b.name, r.business_id
LIMIT 0,30