简体   繁体   中英

Count number of weekdays from the dates passed in where clause in DB2

How to count the number of weekdays for a given date range passed in where clause. For example in the below example there are 13 records however only 9 of them fall under weekday.

select * from table_name where startdate > '2019-01-01' and enddate < '2019-01-31' Date Day Asset Price 01-01-2019 Tuesday A 5 01-01-2019 Tuesday A 23 02-01-2019 Wednesday B 20 03-01-2019 Thursday C 87 04-01-2019 Friday D 34 04-01-2019 Friday D 8 05-01-2019 Saturday E 12 05-01-2019 Saturday E 56 06-01-2019 Sunday F 214 07-01-2019 Monday G 32 08-01-2019 Tuesday H 45 09-01-2019 Wednesday I 67

select count(*)
from table_name
where startdate > '2019-01-01' and enddate < '2019-01-31'
and Day not in ("Saturday", "Sunday")

Is this the correct result, if you run it as is?

with table_name (date) as 
(
  values
  '01-01-2019'
, '01-01-2019'
, '02-01-2019'
, '03-01-2019'
, '04-01-2019'
, '05-01-2019'
, '06-01-2019'
, '07-01-2019'
, '08-01-2019'
, '09-01-2019'
)
select 
—- distinct date
count(distinct date)
from table_name
where dayofweek_iso(date(to_date(date, 'DD-MM-YYYY'))) < 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM