Mysql SQL查询从两个日期列获取工作日
我有一个包含两列的sql表。一列名为DateUpdate,另一列名为DateReport。我希望创建第三列,该列保存每行dateUpdated和dateReported之间的工作日整数值。DateUpdate将始终是dateReported值之后或之上的日期。例如,该表可能如下所示:Mysql SQL查询从两个日期列获取工作日,mysql,sql,calendar,Mysql,Sql,Calendar,我有一个包含两列的sql表。一列名为DateUpdate,另一列名为DateReport。我希望创建第三列,该列保存每行dateUpdated和dateReported之间的工作日整数值。DateUpdate将始终是dateReported值之后或之上的日期。例如,该表可能如下所示: DATE UPDATED Date Reported 2015-06-23 2015-06-02 2015-06-05 2015-06-04 2015-06-23 2015-06-15
DATE UPDATED Date Reported
2015-06-23 2015-06-02
2015-06-05 2015-06-04
2015-06-23 2015-06-15
2015-06-17 2015-06-04
2015-06-23 2015-06-27
2015-06-23 2015-06-04
2015-06-08 2015-06-04
第三列应该是每行两列之间的工作日数。我当前的sql如下所示
select date(time_of_update) as updated,
rep.date_reported as dateReported
from history hist left join reports rep
on hist.name = rep.name
left join calendar cal
on rep.date_reported = cal.calendar_date;
我的日历表有一列包含所有日期,还有一个关联列标记这些日期是否为工作日。例如,我可以运行类似于
select sum(is_business_day) from calendar where
calendar_date between '2015-06-02' and '2015-06-23';
我会有16天的假期。我想知道如何合并这两个列,以获得前面提到的两个列以及第三个列
如果子查询是唯一的方法,那么它是可以接受的。谢谢 如果
dt_tbl
是包含日期更新和日期报告的表,而calendar
是包含日历日期的表,则执行以下操作:
select
a.dateUpdated
, a.dateReported
, sum(is_business_day) num_business_days
from
dt_tbl a
left join
calendar c on c.calendar_date between a.dateReported and a.dateUpdated
group by
a.dateUpdated
, a.dateReported