使用sql查找一个月内的工作日

时间:2013-09-11 14:10:26

标签: sql

我正在使用java udf排除sat和sun以获得工作日。我坚持使用sql查询来排除另一个名为holidays的表中的假期。请帮助我。 我有两张表如下: 表格1 trade_date 2013年12月2日 14-02-2013 2013年2月3日 25-04-2013 表2 假期 2013年11月2日 13-02-2013 2013年2月3日 20-04-2013

我的输出应该如下: trade_date business_day 12-02-2013 7 14-02-2013 8 02-03-2013 0(因为是星期六) 25-04-2013 18

我尝试了这个查询:

select d.t_date 
from dates d 
left outer join holidays h on (d.t_date = h.h_date)

感谢。

1 个答案:

答案 0 :(得分:3)

一种方法是使用in子句排除它们:

select d.t_date 
from dates d 
where d.t_date not in (select h.h_date from holidays)

如果这对您不起作用,只需稍微修改当前查询:

select d.t_date 
from dates d 
left outer join holidays h on (d.t_date = h.h_date)
where h.h_date is null

这表示如果holidays表中不匹配,则可以包含该记录。