简体   繁体   English

获取属于每个月末的日期列表?

[英]Get list of dates that falls in End Of each Month?

I need to get end of each month for the past 3 yrs from the current date excluding statutory holidays and weekends using table1 and table2.我需要使用 table1 和 table2 从当前日期开始过去 3 年的每个月底,不包括法定假日和周末。 Table1 has all the dates ranging from year 2025-2017.表 1 包含 2025-2017 年的所有日期。 Table2 has all the statutory holidays for the years ranging from 2025-2017.表 2 列出了 2025-2017 年的所有法定节假日。

How to create SQL script for to attain this result?如何创建 SQL 脚本以获得此结果? Any suggestions could help.任何建议都可以提供帮助。 Expected result would be list of date last 3yrs of endofmonth excluding statutory holidays and weekends.预期结果将是月末最后 3 年的日期列表,不包括法定节假日和周末。

Table 1 has 2 columns, DateId and FullDate column表 1 有 2 列,DateId 和 FullDate 列

 DateID Fulldate

 1010392 2019-12-1
 1010393 2019-12-2
 1010394 2019-12-3
 1010395 2019-12-4
 .
 .
 101086 2019-12-31

Table 2 has 2 columns, DateId and Statutory_Holidays表 2 有 2 列,DateId 和 Statutory_Holidays

 Date ID   Stat_Holidays
 101085     2019-12-25
 101086     2019-12-26

And the returned results should look like返回的结果应该是这样的

WeekDay_FullDate_Past3yrs
2019-12-31
2020-1-31
2020-2-28
2020-3-31

Tried the below:尝试了以下:

 select * from
 ( select a.Date from Table1 a where a.Date <= 
  '20221215' and a.Date >= DATEADD (YEAR, -3, getdate()) ) as t1
 join
 ( select EOMONTH(a.Date) as Date from Table1 a where a.Date <= '20221215' and a.Date >= DATEADD (YEAR, -3, getdate()) ) as t2 on t1.Date = t2.Date 

tried the solution from the below link it dosen't solve my issue.尝试了以下链接中的解决方案,但无法解决我的问题。 I'm looking to get list of last workday of a month(excluding weekends and holiday) for the past 3yrs我正在寻找过去 3 年一个月的最后一个工作日(不包括周末和节假日)的列表
SQL Server - Get Last Business Data excluding holidays and Weekends SQL Server - 获取不包括节假日和周末的最后业务数据

This should work and give you the last working day for each month in your main table.这应该有效,并在主表中为您提供每个月的最后一个工作日。 Just filter by the desired time period:只需按所需时间段过滤:

SELECT TOP 1 WITH TIES FullDate
FROM Table1
WHERE FullDate NOT IN (SELECT Stat_Holidays FROM Table2) -- not holiday
    AND DATEPART(weekday, FullDate) NOT IN (7, 1) -- not saturday and sunday
ORDER BY DENSE_RANK() OVER(PARTITION BY YEAR(FullDate), MONTH(FullDate) ORDER BY FullDate DESC)
    

You can group by month and year and take the max date (excluding holidays and weekends):您可以按月和年分组并取最大日期(不包括节假日和周末):

SET DATEFIRST 1;
DECLARE @CurrentDate DATE = '20221215';

WITH cte
AS
(
    SELECT MAX(Date ) as EOMDate
    FROM Table1 
    WHERE DATEPART(weekday,Date) NOT IN (6,7)
    AND Date NOT IN (SELECT Date FROM Table2)
    GROUP BY YEAR(Date),MONTH(Date)
)
SELECT *
FROM cte
WHERE cte.EOMDate BETWEEN DATEADD(YEAR,-3,@CurrentDate) AND @CurrentDate;

Check this with your table name and column names.用你的表名和列名检查一下。

select  year(dates) _Year ,month(dates) _Month,EOMONTH(dates) endofMOnth from tabledate1 where DATENAME(DW, dates) not in ('Saturday','Sunday') 
and EOMONTH(dates)  not in (select holidaydate from tableholidays)
Group by year(dates),month(dates),EOMONTH(dates) 
order by year(dates) ,month(dates) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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