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

前端 未结 8 2075
轮回少年
轮回少年 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:16

    You might find this really helpful:

    CREATE OR REPLACE FUNCTION working_days(date, date) RETURNS INT AS
    $$
    SELECT COUNT(days)::INT
        FROM generate_series($1, $2, '1 day') AS days
        WHERE EXTRACT(DOW FROM days) NOT IN(0, 6);
    $$
    LANGUAGE 'sql' IMMUTABLE STRICT;
    

提交回复
热议问题