sql >> Database teknologi >  >> RDS >> PostgreSQL

Ekskluder overlappende perioder i tidsaggregatfunktion

Hvad med dette:

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;
 

For dine data vil det returnere:

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

  1. Base64-kodet streng til simpel sql-injektion

  2. Html / Php-formular tilføjes ikke til SQL-databasen

  3. Indsæt i vælg og opdater i en enkelt forespørgsel

  4. Indstil forskellige miljøvariabler ORACLE_HOME og PATH ved hjælp af Ansible