连续日期postgresql

时间:2015-05-05 08:09:40

标签: sql postgresql gaps-and-islands

我需要知道每个文档是否没有连续日期。我有这张桌子:

document | the_day  
     1   | 2015-01-01  
     1   | 2015-01-02  
     1   | 2015-01-03  
     1   | 2015-01-04  
     1   | 2015-01-05  
     1   | 2015-01-06  
     2   | 2015-01-01  
     2   | 2015-01-03  
     2   | 2015-01-04  
     2   | 2015-01-05  
     2   | 2015-01-06  
     3   | 2015-01-01  
     3   | 2015-01-02  
     3   | 2015-01-03  
     3   | 2015-01-04  
     3   | 2015-01-05  
     3   | 2015-01-06  

如您所见,只有一个差距:在文件2中,缺少“2015-01-02”。 我想知道这个差距。 我有这个选择:

SELECT document, the_day, the_day - lag(the_day) OVER w AS gap
              FROM mytable
              where active=true and fault=false
               WINDOW w AS (ORDER BY document,the_day)

这个选择给了我一个日期的注册,以及差距,在大多数情况下是1,但是当结果中的另一个文档开始时,它给了我错误的差距。 我不知道这是正确的方法还是功能...... 这里是构建表的代码:

--Table: public.test_consecutives

--DROP TABLE public.test_consecutives;

CREATE TABLE public.test_consecutives (
  document  integer,
  the_day   date
) WITH (
    OIDS = FALSE
  );

ALTER TABLE public.test_consecutives
  OWNER TO postgres;
INSERT INTO test_consecutives (document, the_day) VALUES
    (1, '2015-01-01'),
    (1, '2015-01-02'),
    (1, '2015-01-03'),
    (1, '2015-01-04'),
    (1, '2015-01-05'),
    (1, '2015-01-06'),
    (2, '2015-01-01'),
    (2, '2015-01-03'),
    (2, '2015-01-04'),
    (2, '2015-01-05'),
    (2, '2015-01-06'),
    (3, '2015-01-01'),
    (3, '2015-01-02'),
    (3, '2015-01-03'),
    (3, '2015-01-04'),
    (3, '2015-01-05'),
    (3, '2015-01-06');

1 个答案:

答案 0 :(得分:3)

如果你没有指定# Sort each vector for i in range( len( vectors ) ): vectors[ i ] = sorted( vectors[ i ] ) # Output [ [0, 1, 2, 3], [0, 1, 2, 4], [4, 5, 6], [4, 5, 13], [5, 8, 9, 17], [5, 12, 13], [7, 8, 9], [7, 10, 11], [7, 11, 14, 15], [7, 14, 15, 16] ] # Save info about indexes ind = [ ( i, vectors[ i ] ) for i in range( len( vectors ) ) ] # Output convert to list with tuples (index,list[i]) [(0, [0, 1, 2, 3]), (1, [0, 1, 2, 4]), (2, [4, 5, 6]), (3, [4, 5, 13]), (4, [5, 8, 9, 17]), (5, [5, 12, 13]), (6, [7, 8, 9]), (7, [7, 10, 11]), (8, [7, 11, 14, 15]), (9, [7, 14, 15, 16])] sort_info = sorted( ind, key = itemgetter( 1 ) ) # Output sort by list first by sorting the first elements on each list # then the second ones etc. For example list [4, 5, 13] comes before # [5, 8, 9, 17] because their first element is 4 < 5. Then # [5, 8, 9, 17] comes before [5, 12, 13] because the second element # is 8 < 12 etc. [(0, [0, 1, 2, 3]), (1, [0, 1, 2, 4]), (2, [4, 5, 6]), (3, [4, 5, 13]), (4, [5, 8, 9, 17]), (5, [5, 12, 13]), (6, [7, 8, 9]), (7, [7, 10, 11]), (8, [7, 11, 14, 15]), (9, [7, 14, 15, 16]) ] PostgreSQL将假设它是整个表。您的查询应包含PARTITION子句:

PARTITION BY