sql >> データベース >  >> RDS >> Sqlserver

2つの日付の間の平日(日曜日、月曜日、火曜日)の数を取得しますSQL

    私が考えていることを考えると あなたが取得しようとしている、これはそれを行う必要があります:

    SET DATEFIRST 1
    
    DECLARE
        @start_date DATETIME,
        @end_date DATETIME
    
    SET @start_date = '2011-07-11'
    SET @end_date = '2011-07-22'
    
    ;WITH Days_Of_The_Week AS (
        SELECT 1 AS day_number, 'Monday' AS day_name UNION ALL
        SELECT 2 AS day_number, 'Tuesday' AS day_name UNION ALL
        SELECT 3 AS day_number, 'Wednesday' AS day_name UNION ALL
        SELECT 4 AS day_number, 'Thursday' AS day_name UNION ALL
        SELECT 5 AS day_number, 'Friday' AS day_name UNION ALL
        SELECT 6 AS day_number, 'Saturday' AS day_name UNION ALL
        SELECT 7 AS day_number, 'Sunday' AS day_name
    )
    SELECT
        day_name,
        1 + DATEDIFF(wk, @start_date, @end_date) -
            CASE WHEN DATEPART(weekday, @start_date) > day_number THEN 1 ELSE 0 END -
            CASE WHEN DATEPART(weekday, @end_date)   < day_number THEN 1 ELSE 0 END
    FROM
        Days_Of_The_Week
    


    1. 各行の一意のタイムスタンプのOracleSqlステートメント

    2. SQLAlchemy + pymysqlエラー:sqlalchemy.util.queue.Empty

    3. 高レベルのPostgresがINSERT/UPDATE速度を低下させていますか?

    4. MySQL整数の符号なし算術問題?