我想按產品分組,並返回它們基於datediff函數使用的平均時間。SQL Server選擇和分組由datediff
這裏是我的查詢:
SELECT products.productNameCommon, datediff(minute,history.timeOut, history.timeIn) as session
FROM FlexLM_history history
JOIN FlexLM_products products ON products.productID=history.productID
where products.productType = 'base' and history.timeIn is Not NULL
GROUP BY products.productNameCommon, datediff(minute,history.timeOut, history.timeIn)
ORDER BY products.productNameCommon
我的結果是這樣的:
productNameCommon | avgSession
----------------- | ----------
Product 1 | 5
Product 1 | 15
Product 1 | 40
Product 2 | 2
Product 2 | 5
Product 3 | 12
Product 3 | 24
我真正需要的是這樣的:
productNameCommon | avgSession
----------------- | ----------
Product 1 | 20
Product 2 | 3.5
Product 3 | 18
如果從GROUP BY中刪除'datediff()',並在列選擇中將'datediff()'包裝在'sum()'中,會發生什麼?即:sum(datediff()) –