SQL组记录按自定义的每周时间范围

时间:2018-10-04 13:12:18

标签: sql sql-server date datetime

这可能很简单,但是我一直在思考问题。

我需要提取某个特定日期范围(2018年1月-2018年3月)之间的所有记录,这些日期范围是星期日中午12点至星期五下午1点。按周分组

SELECT col1, 
       Sum(col2) 
FROM   (SELECT * 
        FROM   (SELECT col1, 
                       col2, 
                       Datename(dw, [date_col]) AS day_of_week, 
                       date_col 
                FROM   table 
                WHERE  Datename(dw, [update_date]) != 'Saturday' 
               --eliminate Saturday records 
               ) a 
        WHERE  ( Cast(update_date AS TIME) < '12:00' 
                 AND day_of_week != 'Sunday' ) 
       --eliminate Sunday records before noon 
       ) b 
WHERE  ( day_of_week != 'Friday' 
          OR Cast(update_date AS TIME) > '13:00' ) 

--eliminate Friday records after 1PM

3 个答案:

答案 0 :(得分:1)

经过编辑以反映DATEPART(dw,...)的正确返回值

您可以执行逻辑以确定在子查询SELECT中的CASE语句中哪些日期/小时组合有效,然后在外部查询中按星期编号分组。这将简化WHERE子句。我发现这种样式的不透明性较小,并且更易于长期维护。

下面的示例已在Microsoft SQL SERVER上进行了测试。

SELECT
    DATEPART(wk, Update_Date) AS GroupByWeek
    , col1
    , SUM(col2)
FROM (
    SELECT
        col1
        , col2
        , Update_Date
        , CASE
            WHEN DATEPART(dw, Update_Date) = 1 AND DATEPART(hh, Update_Date) >= 12 THEN 1
            WHEN DATEPART(dw, Update_Date) = 6 AND DATEPART(hh, Update_Date) < 13 THEN 1
            WHEN DATEPART(dw, Update_Date) BETWEEN 2 AND 5 THEN 1
            ELSE 0
        END AS FallsInWindow
    FROM table
) RowsInWindow
WHERE 
    RowsInWindow.Update_Date BETWEEN '2018-01-01' AND '2018-04-01'
    AND RowsInWindow.FallsInWindow = 1
GROUP BY
    DATEPART(wk, Update_Date)
    , col1
ORDER BY GroupByWeek

答案 1 :(得分:0)

SELECT col1, Sum(col2)
                FROM   [table] 
                WHERE  Datename(dw, [update_date]) <> 'Saturday'
                OR
                NOT ( Cast(update_date AS TIME) < '12:00' 
                      AND Datename(dw, [update_date]) == 'Sunday' )
                OR 
                NOT ( Datename(dw, [update_date]) == 'Friday' 
                      AND Cast(update_date AS TIME) > '13:00' )
group by col1, datepart(week, update_date);

答案 2 :(得分:0)

您可以使用以下条件:

WHERE update_date >= '2018-01-01' -- between Jan 2018
AND   update_date <  '2018-04-01' -- and Mar 2018
AND (
    DATENAME(dw, update_date) = 'Sunday' AND CAST(update_date AS TIME) >= '12:00' OR
    DATENAME(dw, update_date) IN ('Monday', 'Tuesday', 'Wednesday', 'Thursday') OR
    DATENAME(dw, update_date) = 'Friday' AND CAST(update_date AS TIME) < '13:00'
)