Sliding window 滑动窗口

ROWS BETWEEN <start> AND <finish>

PRECEDING: before current row
FOLLOWING: after current row
UNBOUNDED PRECEDING: every row since the beginning
UNBOUNDED FOLLOWING: every rows until the end
CURRENT ROW

例子1

/* Manchester City Home Games */
SELECT date,
       home_goal,
       away_goal,
       SUM(home_goal) 
          OVER(ORDER BY date 
          ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM match
WHERE hometeam_id = 8456 AND season = '2011/2012';

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
从最早的一条数据,一直到当前的数据
Sliding window 滑动窗口

例子2

/* Manchester City Home Games */
SELECT date,
       home_goal,
       away_goal,
       SUM(home_goal) 
          OVER(ORDER BY date 
          ROWS BETWEEN 1 PRECEDING 
          AND CURRENT ROW) AS last2
FROM match
WHERE hometeam_id = 8456
      AND season = '2011/2012';

ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
从当前数据的前一条,一直到当前数据
Sliding window 滑动窗口

例子3

SELECT 
	-- Select the date, home goal, and away goals
	date,
    home_goal,
    away_goal,
    -- Create a running total and running average of home goals
    SUM (home_goal) OVER(ORDER BY date DESC
         ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS running_total,
    AVG(home_goal) OVER(ORDER BY date DESC
         ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS running_avg
FROM match
WHERE 
	awayteam_id = 9908 
    AND season = '2011/2012';

ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
反方向,需要从current row开始算起

上一篇:素数筛总结----基于HDU - 4715(Difference Between Primes)


下一篇:微信上传图文消息素材40007,invalid media_id hint