Working days between two dates in Snowflake

无人久伴 提交于 2021-01-28 06:10:23

问题


Is there any ways to calculate working days between two dates in snowflake without creating calendar table, only using "datediff" function


回答1:


After doing research work on snowflake datediff function, I have found the following conclusions.

  1. DATEDIFF(DAY/WEEK, START_DATE, END_DATE) will calculate difference, but the last date will be considered as END_DATE -1.
  2. DATEDIFF(WEEK, START_DATE, END_DATE) will count number of Sundays between two dates.

By summarizing these two points, I have implemented the logic below.

SELECT 
    ( DATEDIFF(DAY, START_DATE, DATEADD(DAY, 1, END_DATE))
    - DATEDIFF(WEEK, START_DATE, DATEADD(DAY, 1, END_DATE))*2 
    - (CASE WHEN DAYNAME(START_DATE) != 'Sun' THEN 1 ELSE 0 END)
    + (CASE WHEN DAYNAME(END_DATE) != 'Sat' THEN 1 ELSE 0 END)
    ) AS WORKING_DAYS   


来源:https://stackoverflow.com/questions/65803170/working-days-between-two-dates-in-snowflake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!