Getting number of certain days-of-the-week (weekend) from interval in PostgreSQL

前端 未结 8 2077
轮回少年
轮回少年 2021-01-06 13:27

Given 2 timestamps in postgres, how do you calculate the time difference without counting whole Saturdays and Sundays?

OR

How do you count the number of Satu

相关标签:
8条回答
  • 2021-01-06 14:02

    You might find this blogpost helpful: http://www.depesz.com/index.php/2007/12/27/how-many-1sts-of-any-month-were-sundays-since-1901-01-01/

    0 讨论(0)
  • 2021-01-06 14:04

    This should answer the second part of your question:

    create or replace function is_weekend_day(date) returns boolean
     strict immutable language 'sql'
     as $$ select case extract(dow from $1) when 0 then true when 6 then true else false end $$;
    
    create or replace function count_weekend_days(start_date date, end_date date) returns int
     strict immutable language 'sql'
     as $$
    select cast(sum(case when is_weekend_day($1 + ofs) then 1 else 0 end) as int)
    from generate_series(0, $2 - $1) ofs
    $$;
    

    Making a corresponding count_non_weekend_days is simple after that.

    0 讨论(0)
  • 2021-01-06 14:07

    The following function is returning the number of full weekend days between two dates. As you need full days, you can cast the timestamps to dates before calling the function. It returns 0 in case the first date is not strictly before the second.

    CREATE FUNCTION count_full_weekend_days(date, date)
      RETURNS int AS
    $BODY$
      SELECT
        ($1 < $2)::int
          *
        (
          (($2 - $1) / 7) * 2
            + 
          (EXTRACT(dow FROM $1)<6 AND EXTRACT(dow FROM $2)>0 AND EXTRACT(dow FROM $1)>EXTRACT(dow FROM $2))::int * 2
            +
          (EXTRACT(dow FROM $1)=6 AND EXTRACT(dow FROM $2)>0)::int
            +
          (EXTRACT(dow FROM $2)=0 AND EXTRACT(dow FROM $1)<6)::int
        );
    $BODY$
      LANGUAGE 'SQL' IMMUTABLE STRICT;
    

    Examples:

    SELECT COUNT_FULL_WEEKEND_DAYS('2009-04-10', '2009-04-20');
    # returns 4
    
    SELECT COUNT_FULL_WEEKEND_DAYS('2009-04-11', '2009-04-20');
    # returns 3 (11th is Saturday, so it shouldn't be counted as full day)
    
    SELECT COUNT_FULL_WEEKEND_DAYS('2009-04-12', '2009-04-20');
    # returns 2 (12th is Sunday, so it shouldn't be counted as full day)
    
    SELECT COUNT_FULL_WEEKEND_DAYS('2009-04-13', '2009-04-20');
    # returns 2
    

    To obtain the number of days except full weekend days, simply subtract the number of days from the function above:

    SELECT
      '2009-04-20'::date
        -
      '2009-04-13'::date
        -
       COUNT_FULL_WEEKEND_DAYS('2009-04-13', '2009-04-20');
    
    0 讨论(0)
  • 2021-01-06 14:07

    This will count the number of a certain day between two dates:

    -- 0 Sunday
    -- 1 Monday
    -- 2 Tuesday
    -- 3 Wednesday
    -- 4 Thursday
    -- 5 Friday
    -- 6 Saturday
    
    WITH rng AS (
      SELECT
          'march 3 2013'::date AS start,
          'march 3 2014'::date AS end,
          0                    AS day -- Sunday
    )
    
    SELECT count(1)
    FROM rng, generate_series(0, (extract(epoch from age(rng.end, rng.start)) / (60*60*24))::int) AS n
    WHERE extract(dow from rng.start + (n * '1 day'::interval)) = rng.day
    
    0 讨论(0)
  • 2021-01-06 14:08

    (days/7)*2 + number of sat/sun in the last (days%7) days.

    0 讨论(0)
  • 2021-01-06 14:12

    The best solution to this will be to use a calendar table. This is incredibly useful and you can do all sort of interesting things - including counting the number of working days between two dates or counting the number of holidays between two dates.

    This table is usually populated in advance - say for 20 years with the date for well known holidays appropriately tagged. If holidays shift, you maintain the table once in a while to mark the days as holidays. More info here and here. This uses MS SQL Server, but is easily ported as well.

    0 讨论(0)
提交回复
热议问题