sql 移动平均

WITH Produce AS
 (SELECT 'kale' as item, 23 as purchases, 'vegetable' as category
  UNION ALL SELECT 'orange', 2, 'fruit'
  UNION ALL SELECT 'cabbage', 9, 'vegetable'
  UNION ALL SELECT 'apple', 8, 'fruit'
  UNION ALL SELECT 'leek', 2, 'vegetable'
  UNION ALL SELECT 'lettuce', 10, 'vegetable')
SELECT item, purchases, category, AVG(purchases)
  OVER (
    ORDER BY purchases
    ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
  ) AS avg_purchases
FROM Produce

(an-1+an+an+1)/3

itempurchasescategoryavg_purchases移动平均计算方法
orange2fruit22/1
leek2vegetable4(2+2+8)/3
apple8fruit6.33333
cabbage9vegetable9
lettuce10vegetable14
kale23vegetable16.5

±------------------------------------------------------+

posted @ 2022-08-19 22:50  luoganttcc  阅读(160)  评论(0)    收藏  举报