2016-10-17 185 views
2

有如下表格。SQL加權平均值

make | model | engine | cars_checked | avg_mileage 
---------------------------------------|-------- 
suzuki | sx4 | petrol | 11   | 12 
suzuki | sx4 | diesel | 150   | 16 
suzuki | swift | petrol | 140   | 15 
suzuki | swift | diesel | 18   | 19 
toyota | prius | petrol | 16   | 17 
toyota | prius | hybrid | 250   | 24 

所需的輸出是

  1. 平均里程由發動機(汽油,柴油)
  2. 平均行駛里程爲通過使
  3. 平均里程由模型

不能做一個簡單的group by作爲每個記錄的樣本數(cars_checked)的重量年齡以避免平均值問題的平均值。

什麼是實現它的正確途徑?有沒有辦法考慮在group by中做加權平均的樣本數量?相加的輸出格式爲#1的上方作爲例子

engine | mileage_by_engine 
-------------------------- 
petrol | xx.z 
diesel | yy.z 
+0

你能展示輸出應該是什麼樣子嗎? –

+0

@vkp - 用輸出格式更新 – user3206440

回答

3
SELECT engine, SUM(cars_checked * avg_mileage)/SUM(cars_checked) AS avgMilageByEngine 
FROM [YOUR_TABLE] 
GROUP BY engine 

SELECT make, SUM(cars_checked * avg_mileage)/SUM(cars_checked) AS avgMilageByMake 
FROM [YOUR_TABLE] 
GROUP BY make 

SELECT model, SUM(cars_checked * avg_mileage)/SUM(cars_checked) AS avgMilageByModel 
FROM [YOUR_TABLE] 
GROUP BY model 
1

一種方法以簡化查詢是使用grouping sets -

更新

select engine, make, model, 
     sum(cars_check * avg_mileage)/sum(cars_checked) as avgMilage 
from t 
group by grouping sets ((engine), (make), (model)); 

輸出格式具有非-NULL值僅在列而非被彙總。