如何查找两个日期之间的所有星期五和节假日

发布于 2024-12-29 05:12:32 字数 552 浏览 1 评论 0原文

表:

hDate      Holiday

17/12/2011 National Day
01/01/2012 New Year
....

从表中,我想找到两个日期之间的假期总数:

类似的查询:

select count(hdate)
from table1
where hdate between '" & start_date & "' and '" & end_date & "'

用户输入:

start_date = '16/12/2011' 
end_date = '15/01/2012' 

而且我想找到两个日期之间的星期五。

要查找星期五,如何创建查询?

预期输出:

Holiday Friday

2       5

[2] - 表 1 中的 2 天假期,[5] - 星期五 5 天

如何做到这一点?

The table:

hDate      Holiday

17/12/2011 National Day
01/01/2012 New Year
....

From the table, i want to find the total number of holidays between two dates:

A query like:

select count(hdate)
from table1
where hdate between '" & start_date & "' and '" & end_date & "'

User input:

start_date = '16/12/2011' 
end_date = '15/01/2012' 

and also I want to find the friday between 2 dates.

For finding fridays, how to create a query?

Expected output:

Holiday Friday

2       5

[2] - 2 days holiday from table1, [5] - 5 days friday

How to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

酒几许 2025-01-05 05:12:32

这计算了两个日期之间的星期五:

declare @from datetime= '2012-01-26'  
declare @to datetime  = '2012-01-28'

select datediff(day, -3, @to)/7-datediff(day, -2, @from)/7 

假期很容易找到,看起来您已经涵盖了该部分。

我之前已经回答过这个问题了。但没有得到任何信用:

如何计算 TSQL 中两个日期之间的“星期二”数量?

This counts the fridays between 2 dates:

declare @from datetime= '2012-01-26'  
declare @to datetime  = '2012-01-28'

select datediff(day, -3, @to)/7-datediff(day, -2, @from)/7 

The holidays are easy to find, it seems like you have that part covered already.

I sort of answered this earlier. But didn't get any credit:

How to calculate the number of "Tuesdays" between two dates in TSQL?

捂风挽笑 2025-01-05 05:12:32

该选择可以帮助您:

DECLARE @FROMDATE DATE = '2009-01-07'
DECLARE @TODATE DATE = '2012-01-26'

SELECT COUNT(*) holidays,(select COUNT(*) from table1 where DATEPART(DW, hdate) = 5
AND DT BETWEEN @FROMDATE AND @TODATE ) fridays FROM table1 
WHERE hdate BETWEEN @FROMDATE AND @TODATE

That select help you:

DECLARE @FROMDATE DATE = '2009-01-07'
DECLARE @TODATE DATE = '2012-01-26'

SELECT COUNT(*) holidays,(select COUNT(*) from table1 where DATEPART(DW, hdate) = 5
AND DT BETWEEN @FROMDATE AND @TODATE ) fridays FROM table1 
WHERE hdate BETWEEN @FROMDATE AND @TODATE
合约呢 2025-01-05 05:12:32

请参阅:

为什么应该我考虑使用辅助日历表?

日历表可以发挥很多作用
更容易围绕任何问题开发解决方案
涉及日期的商业模式。
我上次检查过,这包括
几乎任何你可以的商业模式
想想,在某种程度上。持续的
最终需要的问题
冗长、复杂且低效
方法包括以下几种
问题:

  • x 和 y 之间有多少个工作日?
  • 三月的第二个星期二和四月的第一个星期五之间的所有日期是哪些?
  • 我预计这批货物会在什么日期送达?
  • 本季度所有星期五的日期是哪一天?
  • <李>...
     

See:

Why should I consider using an auxiliary calendar table?

A calendar table can make it much
easier to develop solutions around any
business model which involves dates.
Last I checked, this encompasses
pretty much any business model you can
think of, to some degree. Constant
problems that end up requiring
verbose, complicated and inefficient
methods include the following
questions:

  • How many business days between x and y?
  • What are all of the dates between the second Tuesday of March and the first Friday in April? 
  • On what date should I expect this shipment to arrive? 
  • What were the dates of all the Fridays in this quarter? 
  • ...  
人间不值得 2025-01-05 05:12:32
  1. 对于假期,您的 SQL 看起来不错,只是您似乎在将参数插入 SQL 时遇到了麻烦。如果您指定您正在使用哪种编程语言,我们也许可以在这里提供帮助。如果您使用 .NET,则应该使用参数化查询而不是字符串替换。

  2. 对于周五,请参阅此问题:

  1. For the holiday, your SQL looks fine, you just seem to have trouble plugging the parameters into the SQL. If you specify which programming language you are using, we might be able to help here. If you use .NET, you should use Parameterized Queries instead of string substitution.

  2. For the fridays, see this question:

野侃 2025-01-05 05:12:32
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate ='16/12/2011' 
SET @EndDate = '15/01/2012' 


SELECT
   (DATEDIFF(dd, @StartDate, @EndDate) + 1)
  -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
  -(CASE WHEN DATENAME(dw, @StartDate) = 'Friday' THEN 1 ELSE 0 END)
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate ='16/12/2011' 
SET @EndDate = '15/01/2012' 


SELECT
   (DATEDIFF(dd, @StartDate, @EndDate) + 1)
  -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
  -(CASE WHEN DATENAME(dw, @StartDate) = 'Friday' THEN 1 ELSE 0 END)
复古式 2025-01-05 05:12:32
select count(Holiday) as holiday
from Table1
where date between start_date AND end_date
select count(Holiday) as holiday
from Table1
where date between start_date AND end_date
浅唱々樱花落 2025-01-05 05:12:32

我们用额外的时间表来解决这个问题。看起来像这样

ID   | Date        | Holiday   | Year    | CalendarWeek | DayName
1    | 17/12/2011  | 1         | 2011    | 50           | Monday 
2    | 18/12/2011  | 0         | 2011    | 50           | Thursday
3    | 19/12/2011  | 0         | 2011    | 50           | Wendsday

通过这个表,您可以像这样解决您的问题

select 
(select count(d.DayName) from date_table as d 
     where d.DayName = 'Friday' and date >= start_date and date <= end_date ),
(select sum(d.Holiday) from date_table as d 
     where date >= start_date and date <= end_date )

这也应该与 SQL Server 2000 兼容。对于 SQL Server 2005 及以上版本:

with tmp(id) as
(
 select id from  from date_table where date >= start_date and date <= end_date
)
select
 (select count(d.DayName) from date_table inner join tmp on tmp.id = id 
     where DayName = 'Friday' ),
 (select sum(d.Holiday) from date_table inner join tmp on tmp.id = id )

We solving the problem with an extra time table. This looks like this

ID   | Date        | Holiday   | Year    | CalendarWeek | DayName
1    | 17/12/2011  | 1         | 2011    | 50           | Monday 
2    | 18/12/2011  | 0         | 2011    | 50           | Thursday
3    | 19/12/2011  | 0         | 2011    | 50           | Wendsday

With this table you could resolve your question like this

select 
(select count(d.DayName) from date_table as d 
     where d.DayName = 'Friday' and date >= start_date and date <= end_date ),
(select sum(d.Holiday) from date_table as d 
     where date >= start_date and date <= end_date )

This should also be SQL Server 2000 compatible. And this for SQL Server 2005 and above:

with tmp(id) as
(
 select id from  from date_table where date >= start_date and date <= end_date
)
select
 (select count(d.DayName) from date_table inner join tmp on tmp.id = id 
     where DayName = 'Friday' ),
 (select sum(d.Holiday) from date_table inner join tmp on tmp.id = id )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文