aboutsummaryrefslogtreecommitdiff
path: root/challenge-198/luca-ferrari/postgresql/ch-1.sql
blob: b5b1e7fdbb1e8f1f78b2aae297646c970292495a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
CREATE SCHEMA IF NOT EXISTS pwc198;

CREATE OR REPLACE FUNCTION
pwc198.task1_plpgsql( l int[] )
RETURNS int
AS $CODE$

   with counting as (
      select v, v - lag( v, 1, v ) over w as d
      from unnest( l ) v
      window w as (order by v asc )
   )
   , max_counting as (
     select max( d ) from counting
   )
   select count(*)
   from counting
   where d = ( select * from max_counting );


$CODE$
LANGUAGE sql;