小编Ras*_*ash的帖子

计算postgreSQL中2个日期之间的工作日

我正在尝试计算 PostgreSQL 中某个日期范围之间的工作日。

我的桌子:

|start_date |end_date |
------------------------
|2017-06-01 |2017-06-01| 
|2017-05-29 |2017-06-02| 
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

SELECT pto.start_date, pto.end_date, 
SUM(CASE WHEN extract (dow FROM foo) IN(1,2,3,4,5) THEN 1 ELSE 0 END) as theDIFF 
FROM (
  SELECT start_date, (start_date::date + (generate_series(0,end_date::date 
        - start_date::date)||'days')::interval) AS foo
    FROM pto
) foo inner join pto pto
on pto.start_date = foo.start_date 
group by pto.start_date, pto.end_date
Run Code Online (Sandbox Code Playgroud)

我的输出:

      |start_date(date)| end_date(date) |theDiff(integer)
      ---------------------------------------------------
      |2017-06-01      |  2017-06-01    |        29     |
      |2017-05-29      |  2017-06-02    |        12     |
      ---------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

预期输出:

      |start_date(date)| end_date(date) |theDiff(integer)
      --------------------------------------------------- …
Run Code Online (Sandbox Code Playgroud)

postgresql

7
推荐指数
2
解决办法
2万
查看次数

标签 统计

postgresql ×1