sql >> データベース >  >> RDS >> PostgreSQL

時間集計関数で重複する期間を除外する

    これはどうですか:

    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;
    

    データの場合は次のように返されます:

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


    1. 大きなOracle番号を切り捨て/丸めるヒキガエル?

    2. Heroku-ActiveRecord ::StatementInvalid(PG ::Error:ERROR:要求された列が存在しません

    3. 行1の列'profile_pic'のデータが切り捨てられましたimmysql

    4. 月と日を減算するmysql