Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

Determine number working day between two dates - response (4) by dnoeth

$
0
0

Hi Vlad,
this can be further simplified, you don't the sys_calendar:

SELECT start_date, end_date, 
    end_date-start_date+1 AS period_length, 
    period_length - holiday_days AS working_days,
    SUM(CASE WHEN b.bank_holiday='Y' THEN 1 ELSE 0 END) AS holiday_days
FROM a LEFT JOIN b
ON b.hol_date BETWEEN a.start_date AND a.end_date
GROUP BY 1,2
ORDER BY 1,2

 
Of course this kind of calculation involves a CROSS join due to non-equality, better don't try it on a huge table :-)
 
When this calculation must be done repeatedly i usually recommend adding the holiday information to your (hopefully) existing calendar table.
Then add another INT column which is populated using 

SUM(CASE WHEN bank_holiday = 'Y'  THEN 0 ELSE 1 END) OVER (ORDER BY calendar_date ROWS UNBOUNDED PRECEDING) AS WorkDay#

Now you got a running number for each date which is only increased for working days and the calculation uses two equi-joins (instead of the bad product join) and a simple a.WorkDay# - b.WorkDay#
 
Dieter
 


Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>