sql >> Databasteknik >  >> RDS >> PostgreSQL

Exkludera överlappande perioder i tidsaggregationsfunktionen

Vad sägs om detta:

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;

För din data kommer den att returnera:

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


  1. Hur man återställer en trunkerad tabell

  2. Spara utdata från sql-funktionen till csv-fil (COPY) med dynamiskt filnamn

  3. Laravel vänster Gå med i den sista posten i höger tabell

  4. Problemhanteringsdatum i doktrin php