从DB2中where子句中传递的日期算起工作日数

时间:2019-09-06 15:53:07

标签: sql db2

如何计算在where子句中传递的给定日期范围内的工作日数。例如,在下面的示例中,有13条记录,但是其中只有9条属于工作日。

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果按原样运行,这是正确的结果吗?

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