如何计算一个月中的星期日并选择第三个?

时间:2019-03-29 13:59:55

标签: sql-server

我有这张桌子:

Date                 month                      
2014-01-19            1                             
2014-02-16            2                             

我想获得本月的所有第三个星期日,所以:

SELECT CAST(
        CASE
            WHEN [DayOfWeek] = 7
                THEN 1
            ELSE 0
        END AS bit) as Result, [date], [month]
FROM mytable

我从开始做起:

Result                   date                  month
0                       2014-01-01               1
0                       2014-01-02               1
0                       2014-01-03               1
0                       2014-01-04               1
1                       2014-01-05               1
....

给予:

virtualenv

所以这给了我一个星期天,我最初的想法是计算每个月的Result为1并获得第三个日期

如何在sql server中做到这一点?

4 个答案:

答案 0 :(得分:5)

尝试一下。第三个星期日只能在15日到21日之间。

SELECT CAST(
        CASE
            WHEN [DayOfWeek] = 7 AND DatePart(day,[Date]) between 15 and 21
                THEN 1
            ELSE 0
        END AS bit) as Result, [date], [month]
FROM mytable

答案 1 :(得分:1)

类似的事情应该起作用:

select
    a.[Date],
    a.[Month]
from (
    select
        [Date],
        [DayOfWeek],
        ROW_NUMBER() over(partition by [DayOfWeek], [Month] order by [Date]) as DayOfWeekCount,
        [Month]
    from mytable
) a
where
    a.[DayOfWeek] = 7
    and a.DayOfWeekCount = 3

编辑:刚刚看到您已经将DayOfWeek作为表中的字段,因此根本不需要DATEPART。

答案 2 :(得分:0)

以下脚本将给您指定时期内的所有第三个星期日:

USE TEMPDB

DECLARE @StartDate DATE
DECLARE @EndDate DATE

SET @StartDate = '2000-01-01'  
SET @EndDate  = '2040-12-31'  

IF OBJECT_ID ('TEMPDB..#Date') IS NOT NULL DROP TABLE #Date
IF OBJECT_ID ('TEMPDB..#Date') IS NULL CREATE TABLE #Date (Date DATE)

INSERT INTO #Date VALUES (@StartDate)

WHILE @StartDate < @EndDate

BEGIN
INSERT INTO #Date

SELECT DATEADD (DD, 1, @StartDate) AS Date

SET @StartDate = DATEADD (DD, 1, @StartDate)

END;

WITH CTE AS 
(
    SELECT *,  
          CASE WHEN DATEPART (DW, Date) = 7 THEN 'Sunday' ELSE NULL END AS DayOfTheWeek
    FROM #Date AS D
)
, CTE2 AS 
(
    SELECT *,
         ROW_NUMBER() OVER (PARTITION BY YEAR (Date) * 100 + MONTH (Date) ORDER BY Date) AS RowNumb
    FROM CTE 
    WHERE DayOfTheWeek = 'Sunday'
)

SELECT * FROM CTE2 WHERE RowNumb = 3

答案 3 :(得分:0)

这可以通过使用如下所示的row_number分区来实现。

Declare @table1 As table
    (
     Date varchar(14),
     month varchar(15),
     DayOfWeek varchar(15)
    )


BEGIN
Insert Into @table1(date,month,DayOfWeek) values('2014-01-01',1,1)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-02',1,2)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-03',1,3)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-04',1,4)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-05',1,1)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-06',1,2)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-07',1,3)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-08',1,4)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-11',1,1)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-12',1,2)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-13',1,3)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-14',1,4)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-15',1,1)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-16',1,2)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-17',1,3)
Insert Into @table1(date,month,DayOfWeek) values('2014-01-18',1,4)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-01',2,1)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-02',2,2)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-03',2,3)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-04',2,4)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-05',2,1)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-06',2,2)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-07',2,3)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-08',2,4)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-11',2,1)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-12',2,2)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-24',2,3)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-14',2,4)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-15',2,1)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-16',2,2)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-17',2,3)
Insert Into @table1(date,month,DayOfWeek) values('2014-02-18',2,4)

END

Select Date,Month from (
Select *,row_number() over (partition by DayOfWeek,month order by Date) as seqnum from @table1) t2
where seqnum =3 and Dayofweek=3

输出

Date    Month
2014-01-13  1
2014-02-17  2