这是How to find max records for given range的后续问题
我有几个人工输入的范围,比如1-100,101-10001。对于每个范围,我要计算rate - avg(rate for each range)。
输入:
Distance Rate
10 5
25 200
50 300
1000 5
2000 2000输出:
Distance rate - avg(rate for each range)
10 x
25 xx
50 xx
1000 xx
2000 xxx发布于 2017-06-28 05:29:03
您需要定义范围,然后使用窗口函数。这很简单:
select t.distance, t.rate, v.grp,
(t.rate - avg(t.rate) over (partition by v.grp)) as deviation
from t outer apply
(values (case when t.distance <= 100 then '1-100'
when t.distance <= 1000 then '101-1000'
else 'other'
end)
) v(grp);https://stackoverflow.com/questions/44790150
复制相似问题