Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL and Sequential Data

I have a dataset that contains:

Table { date itemName }

The date for the most part is sequential. There are no duplicates of the date [as it is the primary key].

The question is split up into multiple parts (all with respect to using SQL):

  1. Is it possible to find gaps in the date series listed in the table? For example: Dates 1/2/09-1/3/09 are missing
  2. Is it possible to find sections of dates that are missing from the table, that has a range greater than n (this is a number determined at run time)? For example: For n = 2 Dates 1/2/09-1/3/09 are not returned but Dates 5/6/09-6/1/09 are.
like image 209
monksy Avatar asked Oct 05 '09 01:10

monksy


People also ask

Does PostgreSQL support sequence?

A sequence in PostgreSQL is a user-defined schema-bound object that yields a sequence of integers based on a specified specification. The CREATE SEQUENCE statement is used to create sequences in PostgreSQL. Now let's analyze the above syntax: First, set the name of the sequence after the CREATE SEQUENCE clause.

What is sequential scan in PostgreSQL?

A Sequential Scan (or Seq Scan) reads the rows from the table, in order. Sequential scans can be the fastest way of getting a row from a small table, and the fastest way of getting a high proportion of the rows from larger tables.

What is the difference between serial and sequence in PostgreSQL?

There is essentially no difference.


1 Answers

If you can use PostgreSQL 8.4 then window functions will help:

SELECT *
    FROM (SELECT itemName, date, date - lag(date) OVER w AS gap
              FROM someTable WINDOW w AS (ORDER BY date)
         ) AS pairs
    WHERE pairs.gap > '1 day'::interval;
like image 109
Ants Aasma Avatar answered Nov 14 '22 23:11

Ants Aasma