如何在每个月的列中获取最大星期六日期,而无需硬编码

Posted

技术标签:

【中文标题】如何在每个月的列中获取最大星期六日期,而无需硬编码【英文标题】:How to get max Saturday dates in a column of each month, without hardcoding 【发布时间】:2021-11-16 11:59:31 【问题描述】:

如何在 SQL Server 的每个月的列中获取最大星期六日期。谁能帮帮我。

现在我只需要每月最后一个星期六的日期。 例如,

桌子有

07-08-2021 - Saturday
14-08-2021 - Saturday
21-08-2021 - Saturday
28-08-2021 - Saturday

04-09-2021 - Saturday
11-09-2021 - Saturday
18-09-2021 - Saturday
25-09-2021 - Saturday

假设我们在八月,我需要选择该月的最后一个星期六(仅 28-08-2021) 假设我们在九月,我需要选择那个月的最后一个星期六(仅 25-09-2021)

输出:

28-08-2021
25-09-2021

【问题讨论】:

你需要统计每个月的星期六吗? 请向minimal reproducible example 提供样本数据、预期结果和您的尝试。 请检查我是否更新了问题 【参考方案1】:

这是一个计算第N个工作日的函数——可以从月初开始计算,也可以从月底开始计算。

  Alter Function dbo.fnGetNthWeekDay (
        @theDate datetime
      , @theWeekday int
      , @theNthDay int
        )
Returns Table
     As
 Return 

/* 
   Adapted from a version published by Peter Larrson - with minor modifications for performance
   and restructured to eliminate usage of a derived table.

   Inputs:
        @theDate        any date in the month
        @theWeekday     the weekday to calculate: 1 = Monday
                                                  2 = Tuesday
                                                  3 = Wednesday
                                                  4 = Thursday
                                                  5 = Friday
                                                  6 = Saturday
                                                  7 = Sunday
        @theNthDay      the week count where positive is from beginning of the month
                        and negative is from end of month

   Outputs:
        @theDate        the date entered
        @theNthDate     the Nth date of the month
*/

 Select theDate = @theDate
      , dt.nthDate
   From (Values (dateadd(month, datediff(month, @theNthDay, @theDate), 0)))                             As mm(FirstOfMonth)
  Cross Apply (Values (dateadd(day, 7 * @theNthDay - 7 * sign(@theNthDay + 1)
            + (@theWeekday + 6 - datediff(day, -53690, mm.FirstOfMonth) % 7) % 7, mm.FirstOfMonth)))    As dt(nthDate)
  Where @theWeekday Between 1 And 7
    And datediff(month, dt.nthDate, @theDate) = 0
    And @theNthDay In (-5, -4, -3, -2, -1, 1, 2, 3, 4, 5);
Go

你可以这样称呼它:

Select * From dbo.fnGetNthWeekDay('2021-08-15', 6, -1) nwd

【讨论】:

【参考方案2】:

定义 CTE 以填充整个月的日期名称,然后过滤需求。

;with GetDates As (  
        select CAST('01-01-2021' as date) as StartDate, datename(dw, '09-01-2021') as Day_Name
        UNION ALL  
        select DATEADD(day,1, StartDate), datename(dw, DATEADD(day,1, StartDate))
        from GetDates  
        where StartDate < '09-30-2021'  
    )
    
    select max(StartDate)
    from GetDates where Day_Name = 'Saturday'
    group by month(StartDate)
    OPTION (MAXRECURSION 500)

【讨论】:

【参考方案3】:

假设您的表中有一个日期字段(我将在下面的查询中引用它)

with week as (
    select
      date_trunc('week', datefield + interval '2 day') - interval '2 day' as week_date
      -- ^this adjusts the week date_trunc to start on Saturday (it starts on Monday by default)
    
    from sometable  
)

select
    extract(month from week_date) as month_num,
    max(week_date) as last_saturday
    
from week
group by month_num

注意:如果您只有当月的部分数据,则需要稍微更改此查询,但您并没有给我太多内容

【讨论】:

以上是关于如何在每个月的列中获取最大星期六日期,而无需硬编码的主要内容,如果未能解决你的问题,请参考以下文章

如何在不同的列中显示包含每个月值的汇总数据?

从给定日期获取每月的星期数

js获取当前月的1号是星期几

如何将日期,年,月的不同列合并/合并到单个列中

安排Cron跳过每个月的第一个星期六

获取日历月的第一个星期一