我一直在努力為一組相同的 fix_ids 值選擇一個平均值,在另一列上有最大值,最終有人幫助了我,我得到了這段代碼select fix_id , timestamp , avg(age) from t where age > 0 and timestamp = (select max(t2.timestamp) from t t2 where t2.fix_id = t.fix_id)group by fix_id;這完全符合預期,但是,我需要為多個列選擇相同的平均值,我想知道是否有一種方法可以在一個查詢中執行此操作。我可以avg(age),avg(height)但是由于我跳過了年齡列高度為 0 的行,因此這些行將丟失。
1 回答

撒科打諢
TA貢獻1934條經驗 獲得超2個贊
使用條件聚合:
select fix_id, timestamp,
avg(case when age > 0 then age end) as avg_age,
avg(height) as avg_height
from t
where timestamp = (select max(t2.timestamp) from t t2 where t2.fix_id = t.fix_id)
group by fix_id;
- 1 回答
- 0 關注
- 120 瀏覽
添加回答
舉報
0/150
提交
取消