sql >> Base de Datos >  >> RDS >> PostgreSQL

postgresql:dividir el período de tiempo en el evento

Puedes hacer UNION en cambio. De esta manera no genera filas innecesarias

SELECT country, start_date, 
       CASE WHEN event_date BETWEEN start_date AND end_date 
            THEN event_date - 1 
            ELSE end_date
       END AS end_date, event_date
  FROM table1
 UNION ALL
SELECT country, event_date, end_date, event_date
  FROM table1
 WHERE event_date BETWEEN start_date AND end_date
 ORDER BY country, start_date, end_date, event_date

Aquí hay un SQLFiddle demostración

Salida:

| country | start_date |   end_date | event_date |
|---------|------------|------------|------------|
|       A | 1960-01-01 | 1994-07-19 | 1994-07-20 |
|       A | 1994-07-20 | 1999-12-31 | 1994-07-20 |
|       B | 1926-01-01 | 1995-12-31 |     (null) |