sql >> Database >  >> RDS >> PostgreSQL

Overlappende perioden uitsluiten in de tijdaggregatiefunctie

Hoe zit het hiermee:

WITH
   /* get all time points where something changes */
   points AS (
       SELECT "startDate" AS p
       FROM temp_period
       UNION SELECT "endDate"
       FROM temp_period
   ),
   /*
    * Get all date ranges between these time points.
    * The first time range will start with NULL,
    * but that will be excluded in the next CTE anyway.
    */
   inter AS (
      SELECT daterange(
                lag(p) OVER (ORDER BY p),
                p
             ) i
      FROM points
   ),
   /*
    * Get all date ranges that are contained
    * in at least one of the intervals.
    */
   overlap AS (
      SELECT DISTINCT i
      FROM inter
         CROSS JOIN temp_period
      WHERE i <@ daterange("startDate", "endDate")
   )
/* sum the lengths of the date ranges */
SELECT sum(age(upper(i), lower(i)))
FROM overlap;

Voor uw gegevens zal het terugkeren:

┌──────────┐
│ interval │
├──────────┤
│ 576 days │
└──────────┘
(1 row)


  1. Waarden doorgeven van PHP naar JavaScript

  2. FROM-component nodig in elke SELECT-instructie

  3. Queryresultaten retourneren in een vooraf gedefinieerde volgorde

  4. Selecteer uit mysql-tabel WAAR field='$array'?