sql 中的导航函数 FIRST_VALUE, LAST_VALUE

FIRST_VALUE 以下示例计算各部门的最快时间,并且计算某个部门么每个员工与第一名的时间差
WITH finishers AS
 (SELECT 'Sophia Liu' as name,
  TIMESTAMP '2016-10-18 2:51:45' as finish_time,
  'F30-34' as division
  UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
  UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
  UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
  UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
  UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
  UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
  UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
  UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
  UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
  FORMAT_TIMESTAMP('%X', finish_time) AS finish_time,
  division,
  FORMAT_TIMESTAMP('%X', fastest_time) AS fastest_time,
  TIMESTAMP_DIFF(finish_time, fastest_time, SECOND) AS delta_in_seconds
FROM (
  SELECT name,
  finish_time,
  division,
  FIRST_VALUE(finish_time)
    OVER (PARTITION BY division ORDER BY finish_time ASC
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS fastest_time
  FROM finishers);


name finish_time division fastest_time delta_in_seconds
Carly Forte 03:08:58 F25-29 03:08:58 0
Sophia Liu 02:51:45 F30-34 02:51:45 0
Nikki Leith 02:59:01 F30-34 02:51:45 436
Jen Edwards 03:06:36 F30-34 02:51:45 891
Meghan Lederer 03:07:41 F30-34 02:51:45 956
Lauren Reasoner 03:10:14 F30-34 02:51:45 1109
Lisa Stelzner 02:54:11 F35-39 02:54:11 0
Lauren Matthews 03:01:17 F35-39 02:54:11 426
Desiree Berry 03:05:42 F35-39 02:54:11 691
Suzy Slane 03:06:24 F35-39 02:54:11 733
FIRST_VALUE 以下示例计算各部门的最慢的时间,并且计算某个部门么每个员工与最后一名的时间差
WITH finishers AS
 (SELECT 'Sophia Liu' as name,
  TIMESTAMP '2016-10-18 2:51:45' as finish_time,
  'F30-34' as division
  UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
  UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
  UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
  UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
  UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
  UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
  UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
  UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
  UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
  FORMAT_TIMESTAMP('%X', finish_time) AS finish_time,
  division,
  FORMAT_TIMESTAMP('%X', slowest_time) AS slowest_time,
  TIMESTAMP_DIFF(slowest_time, finish_time, SECOND) AS delta_in_seconds
FROM (
  SELECT name,
  finish_time,
  division,
  LAST_VALUE(finish_time)
    OVER (PARTITION BY division ORDER BY finish_time ASC
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS slowest_time
  FROM finishers);



name finish_time division slowest_time delta_in_seconds
Carly Forte 03:08:58 F25-29 03:08:58 0
Sophia Liu 02:51:45 F30-34 03:10:14 1109
Nikki Leith 02:59:01 F30-34 03:10:14 673
Jen Edwards 03:06:36 F30-34 03:10:14 218
Meghan Lederer 03:07:41 F30-34 03:10:14 153
Lauren Reasoner 03:10:14 F30-34 03:10:14 0
Lisa Stelzner 02:54:11 F35-39 03:06:24 733
Lauren Matthews 03:01:17 F35-39 03:06:24 307
Desiree Berry 03:05:42 F35-39 03:06:24 42
Suzy Slane 03:06:24 F35-39 03:06:24 0

你可能感兴趣的:(sql)