SQL将当月的不完整日期替换为0

时间:2018-01-20 22:15:07

标签: sql sql-server

我有这张桌子

   date     week_day
1-02-2018      4
2-02-2018      5
3-02-2018      6
4-02-2018      7
5-02-2018      1
6-02-2018      2
7-02-2018      3
................
26-02-2018     1
27-02-2018     2
28-02-2018     3 

我需要以下面的形式进入SQL不完整的几周:

   date     week_day
0              1
0              2
0              3
1-02-2018      4
2-02-2018      5
3-02-2018      6
4-02-2018      7
5-02-2018      1
6-02-2018      2
7-02-2018      3
................
26-02-2018     1
27-02-2018     2
28-02-2018     3 
0              4
0              5
0              6 
0              7 

4 个答案:

答案 0 :(得分:0)

这就是你要找的东西:

DECLARE @FirstDate date =
(
    SELECT MIN(DATEADD(DAY, (week_day - 1) * -1, [date]))
    FROM YourTable
)

DECLARE @LastDate date =
(
    SELECT MAX(DATEADD(DAY, 7 - week_day, [date]))
    FROM YourTable
)
;

WITH cteDates
AS
(
    SELECT
        @FirstDate [date]
        , 1 week_day

    UNION ALL

    SELECT
        DATEADD(DAY, 1, [date])
        , CASE WHEN week_day + 1 > 7 THEN 1 ELSE week_day + 1 END
    FROM cteDates
    WHERE [date] < @LastDate
)

SELECT
    T.[date]
    , C.week_day
FROM
    cteDates C
    LEFT JOIN YourTable T ON
        C.[date] = T.[date]
        AND C.week_day = T.week_day

答案 1 :(得分:0)

主要关键是获取(年,月,周)和工作日的笛卡尔联接,然后在相关日期进行左联接。第二个关键是调整星期一到一周的开始。

这是一个适用于MySQL的示例。它有一个我尚未解决的边缘情况。一个月的最后一天以星期日结束的情况。在这种情况下,该月末与下一个月的开始之间有7个零。

select * from (
    select cartesian.`year`
    , cartesian.`month`
    , cartesian.`week`  
    , coalesce(dates.`date`,0) `date`, cartesian.weekday+1 week_day from (
        /* get distinct cartesian results for the (year, month, week) and weekday */
        select `year`
        , `month`
        , case weekday when 6 then `week` - 1 else `week` end `week`
        , weekday
        from (
            select distinct year(`date`) `year`
            , month(`date`) `month`
            , week(`date`) `week`
            , weeks.weekday
            from date_48361641, (
                select 0 weekday union
                select 1 weekday union
                select 2 weekday union
                select 3 weekday union
                select 4 weekday union
                select 5 weekday union
                select 6 weekday
            ) weeks
        ) weeks
        -- order by `year`, `month`, `week`, `weekday`
    ) cartesian
    left join (
        /* adjust for edge case of week 0 */
        select `date`
            , `year`
            , `month`
            , `week` 
            , weekday
        from (
            /* adjust the week to allow for Monday start of week*/
            select `date`
                , `year`
                , `month`
                , case weekday when 6 then `week` - 1 else `week` end `week`
                , weekday
                from (
                select `date`
                , year(`date`) `year`
                , month(`date`) `month`
                , week(`date`) `week`
                , weekday(`date`) weekday
                from date_48361641 dates
                -- order by `date`  
            ) dates
        ) dates
    ) dates
    on dates.`year` = cartesian.`year`
    and dates.`month` = cartesian.`month`
    and dates.`week` = cartesian.`week`
    and dates.`weekday` = cartesian.`weekday`
) results
where results.`week` > -1 
order by 
results.`year`
, results.`month`
, results.`week`
, results.`week_day`

- 结果

year    month   week    date    week_day
2016    1   0   0   1
2016    1   0   0   2
2016    1   0   0   3
2016    1   0   0   4
2016    1   0   2016-01-01  5
2016    1   0   2016-01-02  6
2016    1   0   2016-01-03  7
...
2016    1   4   2016-01-25  1
2016    1   4   2016-01-26  2
2016    1   4   2016-01-27  3
2016    1   4   2016-01-28  4
2016    1   4   2016-01-29  5
2016    1   4   2016-01-30  6
2016    1   4   2016-01-31  7
2016    1   5   0   1
2016    1   5   0   2
2016    1   5   0   3
2016    1   5   0   4
2016    1   5   0   5
2016    1   5   0   6
2016    2   4   0   7
2016    2   5   2016-02-01  1
2016    2   5   2016-02-02  2
2016    2   5   2016-02-03  3
2016    2   5   2016-02-04  4
2016    2   5   2016-02-05  5
2016    2   5   2016-02-06  6
2016    2   5   2016-02-07  7
...
2016    2   9   2016-02-29  1
2016    2   9   0   2
2016    2   9   0   3
2016    2   9   0   4
2016    2   9   0   5
2016    2   9   0   6
2016    3   8   0   7
2016    3   9   0   1
2016    3   9   2016-03-01  2
2016    3   9   2016-03-02  3
2016    3   9   2016-03-03  4
2016    3   9   2016-03-04  5
2016    3   9   2016-03-05  6
2016    3   9   2016-03-06  7
...
2016    3   13  2016-03-28  1
2016    3   13  2016-03-29  2
2016    3   13  2016-03-30  3
2016    3   13  2016-03-31  4
2016    3   13  0   5
2016    3   13  0   6
2016    4   12  0   7
2016    4   13  0   1
2016    4   13  0   2
2016    4   13  0   3
2016    4   13  0   4
2016    4   13  2016-04-01  5
2016    4   13  2016-04-02  6
2016    4   13  2016-04-03  7
...
2016    5   22  2016-05-30  1
2016    5   22  2016-05-31  2
2016    5   22  0   3
2016    5   22  0   4
2016    5   22  0   5
2016    5   22  0   6
2016    6   21  0   7
2016    6   22  0   1
2016    6   22  0   2
2016    6   22  2016-06-01  3
2016    6   22  2016-06-02  4
2016    6   22  2016-06-03  5
2016    6   22  2016-06-04  6
2016    6   22  2016-06-05  7
...
2016    6   26  2016-06-27  1
2016    6   26  2016-06-28  2
2016    6   26  2016-06-29  3
2016    6   26  2016-06-30  4
2016    6   26  0   5
2016    6   26  0   6
2016    7   25  0   7
2016    7   26  0   1
2016    7   26  0   2
2016    7   26  0   3
2016    7   26  0   4
2016    7   26  2016-07-01  5
2016    7   26  2016-07-02  6
2016    7   26  2016-07-03  7
...
2016    7   30  2016-07-25  1
2016    7   30  2016-07-26  2
2016    7   30  2016-07-27  3
2016    7   30  2016-07-28  4
2016    7   30  2016-07-29  5
2016    7   30  2016-07-30  6
2016    7   30  2016-07-31  7
2016    7   31  0   1
2016    7   31  0   2
2016    7   31  0   3
2016    7   31  0   4
2016    7   31  0   5
2016    7   31  0   6
2016    8   30  0   7
2016    8   31  2016-08-01  1
2016    8   31  2016-08-02  2
2016    8   31  2016-08-03  3
2016    8   31  2016-08-04  4
2016    8   31  2016-08-05  5
2016    8   31  2016-08-06  6
2016    8   31  2016-08-07  7
...
2016    8   35  2016-08-29  1
2016    8   35  2016-08-30  2
2016    8   35  2016-08-31  3
2016    8   35  0   4
2016    8   35  0   5
2016    8   35  0   6
2016    9   34  0   7
2016    9   35  0   1
2016    9   35  0   2
2016    9   35  0   3
2016    9   35  2016-09-01  4
2016    9   35  2016-09-02  5
2016    9   35  2016-09-03  6
2016    9   35  2016-09-04  7
...
2016    9   39  2016-09-26  1
2016    9   39  2016-09-27  2
2016    9   39  2016-09-28  3
2016    9   39  2016-09-29  4
2016    9   39  2016-09-30  5
2016    9   39  0   6
2016    10  38  0   7
2016    10  39  0   1
2016    10  39  0   2
2016    10  39  0   3
2016    10  39  0   4
2016    10  39  0   5
2016    10  39  2016-10-01  6
2016    10  39  2016-10-02  7
...
2016    10  44  2016-10-31  1
2016    10  44  0   2
2016    10  44  0   3
2016    10  44  0   4
2016    10  44  0   5
2016    10  44  0   6
2016    11  43  0   7
2016    11  44  0   1
2016    11  44  2016-11-01  2
2016    11  44  2016-11-02  3
2016    11  44  2016-11-03  4
2016    11  44  2016-11-04  5
2016    11  44  2016-11-05  6
2016    11  44  2016-11-06  7
...
2016    11  48  2016-11-28  1
2016    11  48  2016-11-29  2
2016    11  48  2016-11-30  3
2016    11  48  0   4
2016    11  48  0   5
2016    11  48  0   6
2016    12  47  0   7
2016    12  48  0   1
2016    12  48  0   2
2016    12  48  0   3
2016    12  48  2016-12-01  4
2016    12  48  2016-12-02  5
2016    12  48  2016-12-03  6
2016    12  48  2016-12-04  7
2016    12  49  2016-12-05  1
2016    12  49  2016-12-06  2
2016    12  49  2016-12-07  3
2016    12  49  2016-12-08  4
2016    12  49  2016-12-09  5
2016    12  49  2016-12-10  6
2016    12  49  2016-12-11  7
2016    12  50  2016-12-12  1
2016    12  50  2016-12-13  2
2016    12  50  2016-12-14  3
2016    12  50  2016-12-15  4
2016    12  50  2016-12-16  5
2016    12  50  2016-12-17  6
2016    12  50  2016-12-18  7
2016    12  51  2016-12-19  1
2016    12  51  2016-12-20  2
2016    12  51  2016-12-21  3
2016    12  51  2016-12-22  4
2016    12  51  2016-12-23  5
2016    12  51  2016-12-24  6
2016    12  51  2016-12-25  7
2016    12  52  2016-12-26  1
2016    12  52  2016-12-27  2
2016    12  52  2016-12-28  3
2016    12  52  2016-12-29  4
2016    12  52  2016-12-30  5
2016    12  52  2016-12-31  6
2017    1   0   2017-01-01  7
2017    1   1   2017-01-02  1
2017    1   1   2017-01-03  2
2017    1   1   2017-01-04  3
2017    1   1   2017-01-05  4
2017    1   1   2017-01-06  5
2017    1   1   2017-01-07  6
2017    1   1   2017-01-08  7
2017    1   2   2017-01-09  1
2017    1   2   2017-01-10  2
2017    1   2   2017-01-11  3
2017    1   2   2017-01-12  4
2017    1   2   2017-01-13  5
2017    1   2   2017-01-14  6
2017    1   2   2017-01-15  7
2017    1   3   2017-01-16  1
2017    1   3   2017-01-17  2
2017    1   3   2017-01-18  3
2017    1   3   2017-01-19  4
2017    1   3   2017-01-20  5
2017    1   3   2017-01-21  6
2017    1   3   2017-01-22  7
2017    1   4   2017-01-23  1
2017    1   4   2017-01-24  2
2017    1   4   2017-01-25  3
2017    1   4   2017-01-26  4
2017    1   4   2017-01-27  5
2017    1   4   2017-01-28  6
2017    1   4   2017-01-29  7
2017    1   5   2017-01-30  1
2017    1   5   2017-01-31  2
2017    1   5   0   3
2017    1   5   0   4
2017    1   5   0   5
2017    1   5   0   6
2017    2   4   0   7
2017    2   5   0   1
2017    2   5   0   2
2017    2   5   2017-02-01  3
2017    2   5   2017-02-02  4
2017    2   5   2017-02-03  5
2017    2   5   2017-02-04  6
2017    2   5   2017-02-05  7
2017    2   6   2017-02-06  1
2017    2   6   2017-02-07  2
2017    2   6   2017-02-08  3
2017    2   6   2017-02-09  4
2017    2   6   2017-02-10  5
2017    2   6   2017-02-11  6
2017    2   6   2017-02-12  7
2017    2   7   2017-02-13  1
2017    2   7   2017-02-14  2
2017    2   7   2017-02-15  3
2017    2   7   2017-02-16  4
2017    2   7   2017-02-17  5
2017    2   7   2017-02-18  6
2017    2   7   2017-02-19  7
2017    2   8   2017-02-20  1
2017    2   8   2017-02-21  2
2017    2   8   2017-02-22  3
2017    2   8   2017-02-23  4
2017    2   8   2017-02-24  5
2017    2   8   2017-02-25  6
2017    2   8   2017-02-26  7
2017    2   9   2017-02-27  1
2017    2   9   2017-02-28  2
2017    2   9   0   3
2017    2   9   0   4
2017    2   9   0   5
2017    2   9   0   6
2017    3   8   0   7
2017    3   9   0   1
2017    3   9   0   2
2017    3   9   2017-03-01  3
2017    3   9   2017-03-02  4
2017    3   9   2017-03-03  5
2017    3   9   2017-03-04  6
2017    3   9   2017-03-05  7
2017    3   10  2017-03-06  1
2017    3   10  2017-03-07  2
2017    3   10  2017-03-08  3
2017    3   10  2017-03-09  4
2017    3   10  2017-03-10  5
2017    3   10  2017-03-11  6
2017    3   10  2017-03-12  7
2017    3   11  2017-03-13  1
2017    3   11  2017-03-14  2
2017    3   11  2017-03-15  3
2017    3   11  2017-03-16  4
2017    3   11  2017-03-17  5
2017    3   11  2017-03-18  6
2017    3   11  2017-03-19  7
2017    3   12  2017-03-20  1
2017    3   12  2017-03-21  2
2017    3   12  2017-03-22  3
2017    3   12  2017-03-23  4
2017    3   12  2017-03-24  5
2017    3   12  2017-03-25  6
2017    3   12  2017-03-26  7
2017    3   13  2017-03-27  1
2017    3   13  2017-03-28  2
2017    3   13  2017-03-29  3
2017    3   13  2017-03-30  4
2017    3   13  2017-03-31  5
2017    3   13  0   6
2017    4   12  0   7
2017    4   13  0   1
2017    4   13  0   2
2017    4   13  0   3
2017    4   13  0   4
2017    4   13  0   5
2017    4   13  2017-04-01  6
2017    4   13  2017-04-02  7
2017    4   14  2017-04-03  1
2017    4   14  2017-04-04  2
2017    4   14  2017-04-05  3
2017    4   14  2017-04-06  4
2017    4   14  2017-04-07  5
2017    4   14  2017-04-08  6
2017    4   14  2017-04-09  7
2017    4   15  2017-04-10  1
2017    4   15  2017-04-11  2
2017    4   15  2017-04-12  3
2017    4   15  2017-04-13  4
2017    4   15  2017-04-14  5
2017    4   15  2017-04-15  6
2017    4   15  2017-04-16  7
2017    4   16  2017-04-17  1
2017    4   16  2017-04-18  2
2017    4   16  2017-04-19  3
2017    4   16  2017-04-20  4
2017    4   16  2017-04-21  5
2017    4   16  2017-04-22  6
2017    4   16  2017-04-23  7
2017    4   17  2017-04-24  1
2017    4   17  2017-04-25  2
2017    4   17  2017-04-26  3
2017    4   17  2017-04-27  4
2017    4   17  2017-04-28  5
2017    4   17  2017-04-29  6
2017    4   17  2017-04-30  7
2017    4   18  0   1
2017    4   18  0   2
2017    4   18  0   3
2017    4   18  0   4
2017    4   18  0   5
2017    4   18  0   6
2017    5   17  0   7
2017    5   18  2017-05-01  1
2017    5   18  2017-05-02  2
2017    5   18  2017-05-03  3
2017    5   18  2017-05-04  4
2017    5   18  2017-05-05  5
2017    5   18  2017-05-06  6
2017    5   18  2017-05-07  7
2017    5   19  2017-05-08  1
2017    5   19  2017-05-09  2
2017    5   19  2017-05-10  3
2017    5   19  2017-05-11  4
2017    5   19  2017-05-12  5
2017    5   19  2017-05-13  6
2017    5   19  2017-05-14  7
2017    5   20  2017-05-15  1
2017    5   20  2017-05-16  2
2017    5   20  2017-05-17  3
2017    5   20  2017-05-18  4
2017    5   20  2017-05-19  5
2017    5   20  2017-05-20  6
2017    5   20  2017-05-21  7
2017    5   21  2017-05-22  1
2017    5   21  2017-05-23  2
2017    5   21  2017-05-24  3
2017    5   21  2017-05-25  4
2017    5   21  2017-05-26  5
2017    5   21  2017-05-27  6
2017    5   21  2017-05-28  7
2017    5   22  2017-05-29  1
2017    5   22  2017-05-30  2
2017    5   22  2017-05-31  3
2017    5   22  0   4
2017    5   22  0   5
2017    5   22  0   6
2017    6   21  0   7
2017    6   22  0   1
2017    6   22  0   2
2017    6   22  0   3
2017    6   22  2017-06-01  4
2017    6   22  2017-06-02  5
2017    6   22  2017-06-03  6
2017    6   22  2017-06-04  7
2017    6   23  2017-06-05  1
2017    6   23  2017-06-06  2
2017    6   23  2017-06-07  3
2017    6   23  2017-06-08  4
2017    6   23  2017-06-09  5
2017    6   23  2017-06-10  6
2017    6   23  2017-06-11  7
2017    6   24  2017-06-12  1
2017    6   24  2017-06-13  2
2017    6   24  2017-06-14  3
2017    6   24  2017-06-15  4
2017    6   24  2017-06-16  5
2017    6   24  2017-06-17  6
2017    6   24  2017-06-18  7
2017    6   25  2017-06-19  1
2017    6   25  2017-06-20  2
2017    6   25  2017-06-21  3
2017    6   25  2017-06-22  4
2017    6   25  2017-06-23  5
2017    6   25  2017-06-24  6
2017    6   25  2017-06-25  7
2017    6   26  2017-06-26  1
2017    6   26  2017-06-27  2
2017    6   26  2017-06-28  3
2017    6   26  2017-06-29  4
2017    6   26  2017-06-30  5
2017    6   26  0   6
2017    7   25  0   7
2017    7   26  0   1
2017    7   26  0   2
2017    7   26  0   3
2017    7   26  0   4
2017    7   26  0   5
2017    7   26  2017-07-01  6
2017    7   26  2017-07-02  7
2017    7   27  2017-07-03  1
2017    7   27  2017-07-04  2
2017    7   27  2017-07-05  3
2017    7   27  2017-07-06  4
2017    7   27  2017-07-07  5
2017    7   27  2017-07-08  6
2017    7   27  2017-07-09  7
2017    7   28  2017-07-10  1
2017    7   28  2017-07-11  2
2017    7   28  2017-07-12  3
2017    7   28  2017-07-13  4
2017    7   28  2017-07-14  5
2017    7   28  2017-07-15  6
2017    7   28  2017-07-16  7
2017    7   29  2017-07-17  1
2017    7   29  2017-07-18  2
2017    7   29  2017-07-19  3
2017    7   29  2017-07-20  4
2017    7   29  2017-07-21  5
2017    7   29  2017-07-22  6
2017    7   29  2017-07-23  7
2017    7   30  2017-07-24  1
2017    7   30  2017-07-25  2
2017    7   30  2017-07-26  3
2017    7   30  2017-07-27  4
2017    7   30  2017-07-28  5
2017    7   30  2017-07-29  6
2017    7   30  2017-07-30  7
2017    7   31  2017-07-31  1
2017    7   31  0   2
2017    7   31  0   3
2017    7   31  0   4
2017    7   31  0   5
2017    7   31  0   6
2017    8   30  0   7
2017    8   31  0   1
2017    8   31  2017-08-01  2
2017    8   31  2017-08-02  3
2017    8   31  2017-08-03  4
2017    8   31  2017-08-04  5
2017    8   31  2017-08-05  6
2017    8   31  2017-08-06  7
2017    8   32  2017-08-07  1
2017    8   32  2017-08-08  2
2017    8   32  2017-08-09  3
2017    8   32  2017-08-10  4
2017    8   32  2017-08-11  5
2017    8   32  2017-08-12  6
2017    8   32  2017-08-13  7
2017    8   33  2017-08-14  1
2017    8   33  2017-08-15  2
2017    8   33  2017-08-16  3
2017    8   33  2017-08-17  4
2017    8   33  2017-08-18  5
2017    8   33  2017-08-19  6
2017    8   33  2017-08-20  7
2017    8   34  2017-08-21  1
2017    8   34  2017-08-22  2
2017    8   34  2017-08-23  3
2017    8   34  2017-08-24  4
2017    8   34  2017-08-25  5
2017    8   34  2017-08-26  6
2017    8   34  2017-08-27  7
2017    8   35  2017-08-28  1
2017    8   35  2017-08-29  2
2017    8   35  2017-08-30  3
2017    8   35  2017-08-31  4
2017    8   35  0   5
2017    8   35  0   6
2017    9   34  0   7
2017    9   35  0   1
2017    9   35  0   2
2017    9   35  0   3
2017    9   35  0   4
2017    9   35  2017-09-01  5
2017    9   35  2017-09-02  6
2017    9   35  2017-09-03  7
2017    9   36  2017-09-04  1
2017    9   36  2017-09-05  2
2017    9   36  2017-09-06  3
2017    9   36  2017-09-07  4
2017    9   36  2017-09-08  5
2017    9   36  2017-09-09  6
2017    9   36  2017-09-10  7
2017    9   37  2017-09-11  1
2017    9   37  2017-09-12  2
2017    9   37  2017-09-13  3
2017    9   37  2017-09-14  4
2017    9   37  2017-09-15  5
2017    9   37  2017-09-16  6
2017    9   37  2017-09-17  7
2017    9   38  2017-09-18  1
2017    9   38  2017-09-19  2
2017    9   38  2017-09-20  3
2017    9   38  2017-09-21  4
2017    9   38  2017-09-22  5
2017    9   38  2017-09-23  6
2017    9   38  2017-09-24  7
2017    9   39  2017-09-25  1
2017    9   39  2017-09-26  2
2017    9   39  2017-09-27  3
2017    9   39  2017-09-28  4
2017    9   39  2017-09-29  5
2017    9   39  2017-09-30  6
2017    10  39  2017-10-01  7
2017    10  40  2017-10-02  1
2017    10  40  2017-10-03  2
2017    10  40  2017-10-04  3
2017    10  40  2017-10-05  4
2017    10  40  2017-10-06  5
2017    10  40  2017-10-07  6
2017    10  40  2017-10-08  7
2017    10  41  2017-10-09  1
2017    10  41  2017-10-10  2
2017    10  41  2017-10-11  3
2017    10  41  2017-10-12  4
2017    10  41  2017-10-13  5
2017    10  41  2017-10-14  6
2017    10  41  2017-10-15  7
2017    10  42  2017-10-16  1
2017    10  42  2017-10-17  2
2017    10  42  2017-10-18  3
2017    10  42  2017-10-19  4
2017    10  42  2017-10-20  5
2017    10  42  2017-10-21  6
2017    10  42  2017-10-22  7
2017    10  43  2017-10-23  1
2017    10  43  2017-10-24  2
2017    10  43  2017-10-25  3
2017    10  43  2017-10-26  4
2017    10  43  2017-10-27  5
2017    10  43  2017-10-28  6
2017    10  43  2017-10-29  7
2017    10  44  2017-10-30  1
2017    10  44  2017-10-31  2
2017    10  44  0   3
2017    10  44  0   4
2017    10  44  0   5
2017    10  44  0   6
2017    11  43  0   7
2017    11  44  0   1
2017    11  44  0   2
2017    11  44  2017-11-01  3
2017    11  44  2017-11-02  4
2017    11  44  2017-11-03  5
2017    11  44  2017-11-04  6
2017    11  44  2017-11-05  7
2017    11  45  2017-11-06  1
2017    11  45  2017-11-07  2
2017    11  45  2017-11-08  3
2017    11  45  2017-11-09  4
2017    11  45  2017-11-10  5
2017    11  45  2017-11-11  6
2017    11  45  2017-11-12  7
2017    11  46  2017-11-13  1
2017    11  46  2017-11-14  2
2017    11  46  2017-11-15  3
2017    11  46  2017-11-16  4
2017    11  46  2017-11-17  5
2017    11  46  2017-11-18  6
2017    11  46  2017-11-19  7
2017    11  47  2017-11-20  1
2017    11  47  2017-11-21  2
2017    11  47  2017-11-22  3
2017    11  47  2017-11-23  4
2017    11  47  2017-11-24  5
2017    11  47  2017-11-25  6
2017    11  47  2017-11-26  7
2017    11  48  2017-11-27  1
2017    11  48  2017-11-28  2
2017    11  48  2017-11-29  3
2017    11  48  2017-11-30  4
2017    11  48  0   5
2017    11  48  0   6
2017    12  47  0   7
2017    12  48  0   1
2017    12  48  0   2
2017    12  48  0   3
2017    12  48  0   4
2017    12  48  2017-12-01  5
2017    12  48  2017-12-02  6
2017    12  48  2017-12-03  7
2017    12  49  2017-12-04  1
2017    12  49  2017-12-05  2
2017    12  49  2017-12-06  3
2017    12  49  2017-12-07  4
2017    12  49  2017-12-08  5
2017    12  49  2017-12-09  6
2017    12  49  2017-12-10  7
2017    12  50  2017-12-11  1
2017    12  50  2017-12-12  2
2017    12  50  2017-12-13  3
2017    12  50  2017-12-14  4
2017    12  50  2017-12-15  5
2017    12  50  2017-12-16  6
2017    12  50  2017-12-17  7
2017    12  51  2017-12-18  1
2017    12  51  2017-12-19  2
2017    12  51  2017-12-20  3
2017    12  51  2017-12-21  4
2017    12  51  2017-12-22  5
2017    12  51  2017-12-23  6
2017    12  51  2017-12-24  7
2017    12  52  2017-12-25  1
2017    12  52  2017-12-26  2
2017    12  52  2017-12-27  3
2017    12  52  2017-12-28  4
2017    12  52  2017-12-29  5
2017    12  52  2017-12-30  6
2017    12  52  2017-12-31  7
2017    12  53  0   1
2017    12  53  0   2
2017    12  53  0   3
2017    12  53  0   4
2017    12  53  0   5
2017    12  53  0   6
2018    1   0   2018-01-01  1
2018    1   0   2018-01-02  2
2018    1   0   2018-01-03  3
2018    1   0   2018-01-04  4
2018    1   0   2018-01-05  5
2018    1   0   2018-01-06  6
2018    1   0   2018-01-07  7
2018    1   1   2018-01-08  1
2018    1   1   2018-01-09  2
2018    1   1   2018-01-10  3
2018    1   1   2018-01-11  4
2018    1   1   2018-01-12  5
2018    1   1   2018-01-13  6
2018    1   1   2018-01-14  7
2018    1   2   2018-01-15  1
2018    1   2   2018-01-16  2
2018    1   2   2018-01-17  3
2018    1   2   2018-01-18  4
2018    1   2   2018-01-19  5
2018    1   2   2018-01-20  6
2018    1   2   2018-01-21  7
2018    1   3   2018-01-22  1
2018    1   3   2018-01-23  2
2018    1   3   2018-01-24  3
2018    1   3   2018-01-25  4
2018    1   3   2018-01-26  5
2018    1   3   2018-01-27  6
2018    1   3   2018-01-28  7
2018    1   4   2018-01-29  1
2018    1   4   2018-01-30  2
2018    1   4   2018-01-31  3
2018    1   4   0   4
2018    1   4   0   5
2018    1   4   0   6
2018    2   3   0   7
2018    2   4   0   1
2018    2   4   0   2
2018    2   4   0   3
2018    2   4   2018-02-01  4
2018    2   4   2018-02-02  5
2018    2   4   2018-02-03  6
2018    2   4   2018-02-04  7
2018    2   5   2018-02-05  1
2018    2   5   2018-02-06  2
2018    2   5   2018-02-07  3
2018    2   5   2018-02-08  4
2018    2   5   2018-02-09  5
2018    2   5   2018-02-10  6
2018    2   5   2018-02-11  7
2018    2   6   2018-02-12  1
2018    2   6   2018-02-13  2
2018    2   6   2018-02-14  3
2018    2   6   2018-02-15  4
2018    2   6   2018-02-16  5
2018    2   6   2018-02-17  6
2018    2   6   2018-02-18  7
2018    2   7   2018-02-19  1
2018    2   7   2018-02-20  2
2018    2   7   2018-02-21  3
2018    2   7   2018-02-22  4
2018    2   7   2018-02-23  5
2018    2   7   2018-02-24  6
2018    2   7   2018-02-25  7
2018    2   8   2018-02-26  1
2018    2   8   2018-02-27  2
2018    2   8   2018-02-28  3
2018    2   8   0   4
2018    2   8   0   5
2018    2   8   0   6
2018    3   7   0   7
2018    3   8   0   1
2018    3   8   0   2
2018    3   8   0   3
2018    3   8   2018-03-01  4
2018    3   8   2018-03-02  5
2018    3   8   2018-03-03  6
2018    3   8   2018-03-04  7
2018    3   9   2018-03-05  1
2018    3   9   2018-03-06  2
2018    3   9   2018-03-07  3
2018    3   9   2018-03-08  4
2018    3   9   2018-03-09  5
2018    3   9   2018-03-10  6
2018    3   9   2018-03-11  7
2018    3   10  2018-03-12  1
2018    3   10  2018-03-13  2
2018    3   10  2018-03-14  3
2018    3   10  2018-03-15  4
2018    3   10  2018-03-16  5
2018    3   10  2018-03-17  6
2018    3   10  2018-03-18  7
2018    3   11  2018-03-19  1
2018    3   11  2018-03-20  2
2018    3   11  2018-03-21  3
2018    3   11  2018-03-22  4
2018    3   11  2018-03-23  5
2018    3   11  2018-03-24  6
2018    3   11  2018-03-25  7
2018    3   12  2018-03-26  1
2018    3   12  2018-03-27  2
2018    3   12  2018-03-28  3
2018    3   12  2018-03-29  4
2018    3   12  2018-03-30  5
2018    3   12  2018-03-31  6
2018    4   12  2018-04-01  7
2018    4   13  2018-04-02  1
2018    4   13  2018-04-03  2
2018    4   13  2018-04-04  3
2018    4   13  2018-04-05  4
2018    4   13  2018-04-06  5
2018    4   13  2018-04-07  6
2018    4   13  2018-04-08  7
2018    4   14  2018-04-09  1
2018    4   14  2018-04-10  2
2018    4   14  2018-04-11  3
2018    4   14  2018-04-12  4
2018    4   14  2018-04-13  5
2018    4   14  2018-04-14  6
2018    4   14  2018-04-15  7
2018    4   15  2018-04-16  1
2018    4   15  2018-04-17  2
2018    4   15  2018-04-18  3
2018    4   15  2018-04-19  4
2018    4   15  2018-04-20  5
2018    4   15  2018-04-21  6
2018    4   15  2018-04-22  7
2018    4   16  2018-04-23  1
2018    4   16  2018-04-24  2
2018    4   16  2018-04-25  3
2018    4   16  2018-04-26  4
2018    4   16  2018-04-27  5
2018    4   16  2018-04-28  6
2018    4   16  2018-04-29  7
2018    4   17  2018-04-30  1
2018    4   17  0   2
2018    4   17  0   3
2018    4   17  0   4
2018    4   17  0   5
2018    4   17  0   6
2018    5   16  0   7
2018    5   17  0   1
2018    5   17  2018-05-01  2
2018    5   17  2018-05-02  3
2018    5   17  2018-05-03  4
2018    5   17  2018-05-04  5
2018    5   17  2018-05-05  6
2018    5   17  2018-05-06  7
2018    5   18  2018-05-07  1
2018    5   18  2018-05-08  2
2018    5   18  2018-05-09  3
2018    5   18  2018-05-10  4
2018    5   18  2018-05-11  5
2018    5   18  2018-05-12  6
2018    5   18  2018-05-13  7
2018    5   19  2018-05-14  1
2018    5   19  2018-05-15  2
2018    5   19  2018-05-16  3
2018    5   19  2018-05-17  4
2018    5   19  2018-05-18  5
2018    5   19  2018-05-19  6
2018    5   19  2018-05-20  7
2018    5   20  2018-05-21  1
2018    5   20  2018-05-22  2
2018    5   20  2018-05-23  3
2018    5   20  2018-05-24  4
2018    5   20  2018-05-25  5
2018    5   20  2018-05-26  6
2018    5   20  2018-05-27  7
2018    5   21  2018-05-28  1
2018    5   21  2018-05-29  2
2018    5   21  2018-05-30  3
2018    5   21  2018-05-31  4
2018    5   21  0   5
2018    5   21  0   6
2018    6   20  0   7
2018    6   21  0   1
2018    6   21  0   2
2018    6   21  0   3
2018    6   21  0   4
2018    6   21  2018-06-01  5
2018    6   21  2018-06-02  6
2018    6   21  2018-06-03  7
2018    6   22  2018-06-04  1
2018    6   22  2018-06-05  2
2018    6   22  2018-06-06  3
2018    6   22  2018-06-07  4
2018    6   22  2018-06-08  5
2018    6   22  2018-06-09  6
2018    6   22  2018-06-10  7
2018    6   23  2018-06-11  1
2018    6   23  2018-06-12  2
2018    6   23  2018-06-13  3
2018    6   23  2018-06-14  4
2018    6   23  2018-06-15  5
2018    6   23  2018-06-16  6
2018    6   23  2018-06-17  7
2018    6   24  2018-06-18  1
2018    6   24  2018-06-19  2
2018    6   24  2018-06-20  3
2018    6   24  2018-06-21  4
2018    6   24  2018-06-22  5
2018    6   24  2018-06-23  6
2018    6   24  2018-06-24  7
2018    6   25  2018-06-25  1
2018    6   25  2018-06-26  2
2018    6   25  2018-06-27  3
2018    6   25  2018-06-28  4
2018    6   25  2018-06-29  5
2018    6   25  2018-06-30  6
2018    7   25  2018-07-01  7
2018    7   26  2018-07-02  1
2018    7   26  2018-07-03  2
2018    7   26  2018-07-04  3
2018    7   26  2018-07-05  4
2018    7   26  2018-07-06  5
2018    7   26  2018-07-07  6
2018    7   26  2018-07-08  7
2018    7   27  2018-07-09  1
2018    7   27  2018-07-10  2
2018    7   27  2018-07-11  3
2018    7   27  2018-07-12  4
2018    7   27  2018-07-13  5
2018    7   27  2018-07-14  6
2018    7   27  2018-07-15  7
2018    7   28  2018-07-16  1
2018    7   28  2018-07-17  2
2018    7   28  2018-07-18  3
2018    7   28  2018-07-19  4
2018    7   28  2018-07-20  5
2018    7   28  2018-07-21  6
2018    7   28  2018-07-22  7
2018    7   29  2018-07-23  1
2018    7   29  2018-07-24  2
2018    7   29  2018-07-25  3
2018    7   29  2018-07-26  4
2018    7   29  2018-07-27  5
2018    7   29  2018-07-28  6
2018    7   29  2018-07-29  7
2018    7   30  2018-07-30  1
2018    7   30  2018-07-31  2
2018    7   30  0   3
2018    7   30  0   4
2018    7   30  0   5
2018    7   30  0   6
2018    8   29  0   7
2018    8   30  0   1
2018    8   30  0   2
2018    8   30  2018-08-01  3
2018    8   30  2018-08-02  4
2018    8   30  2018-08-03  5
2018    8   30  2018-08-04  6
2018    8   30  2018-08-05  7
2018    8   31  2018-08-06  1
2018    8   31  2018-08-07  2
2018    8   31  2018-08-08  3
2018    8   31  2018-08-09  4
2018    8   31  2018-08-10  5
2018    8   31  2018-08-11  6
2018    8   31  2018-08-12  7
2018    8   32  2018-08-13  1
2018    8   32  2018-08-14  2
2018    8   32  2018-08-15  3
2018    8   32  2018-08-16  4
2018    8   32  2018-08-17  5
2018    8   32  2018-08-18  6
2018    8   32  2018-08-19  7
2018    8   33  2018-08-20  1
2018    8   33  2018-08-21  2
2018    8   33  2018-08-22  3
2018    8   33  2018-08-23  4
2018    8   33  2018-08-24  5
2018    8   33  2018-08-25  6
2018    8   33  2018-08-26  7
2018    8   34  2018-08-27  1
2018    8   34  2018-08-28  2
2018    8   34  2018-08-29  3
2018    8   34  2018-08-30  4
2018    8   34  2018-08-31  5
2018    8   34  0   6
2018    9   33  0   7
2018    9   34  0   1
2018    9   34  0   2
2018    9   34  0   3
2018    9   34  0   4
2018    9   34  0   5
2018    9   34  2018-09-01  6
2018    9   34  2018-09-02  7
2018    9   35  2018-09-03  1
2018    9   35  2018-09-04  2
2018    9   35  2018-09-05  3
2018    9   35  2018-09-06  4
2018    9   35  2018-09-07  5
2018    9   35  2018-09-08  6
2018    9   35  2018-09-09  7
2018    9   36  2018-09-10  1
2018    9   36  2018-09-11  2
2018    9   36  2018-09-12  3
2018    9   36  2018-09-13  4
2018    9   36  2018-09-14  5
2018    9   36  2018-09-15  6
2018    9   36  2018-09-16  7
2018    9   37  2018-09-17  1
2018    9   37  2018-09-18  2
2018    9   37  2018-09-19  3
2018    9   37  2018-09-20  4
2018    9   37  2018-09-21  5
2018    9   37  2018-09-22  6
2018    9   37  2018-09-23  7
2018    9   38  2018-09-24  1
2018    9   38  2018-09-25  2
2018    9   38  2018-09-26  3
2018    9   38  2018-09-27  4
2018    9   38  2018-09-28  5
2018    9   38  2018-09-29  6
2018    9   38  2018-09-30  7
2018    9   39  0   1
2018    9   39  0   2
2018    9   39  0   3
2018    9   39  0   4
2018    9   39  0   5
2018    9   39  0   6
2018    10  38  0   7
2018    10  39  2018-10-01  1
2018    10  39  2018-10-02  2
2018    10  39  2018-10-03  3
2018    10  39  2018-10-04  4
2018    10  39  2018-10-05  5
2018    10  39  2018-10-06  6
2018    10  39  2018-10-07  7
2018    10  40  2018-10-08  1
2018    10  40  2018-10-09  2
2018    10  40  2018-10-10  3
2018    10  40  2018-10-11  4
2018    10  40  2018-10-12  5
2018    10  40  2018-10-13  6
2018    10  40  2018-10-14  7
2018    10  41  2018-10-15  1
2018    10  41  2018-10-16  2
2018    10  41  2018-10-17  3
2018    10  41  2018-10-18  4
2018    10  41  2018-10-19  5
2018    10  41  2018-10-20  6
2018    10  41  2018-10-21  7
2018    10  42  2018-10-22  1
2018    10  42  2018-10-23  2
2018    10  42  2018-10-24  3
2018    10  42  2018-10-25  4
2018    10  42  2018-10-26  5
2018    10  42  2018-10-27  6
2018    10  42  2018-10-28  7
2018    10  43  2018-10-29  1
2018    10  43  2018-10-30  2
2018    10  43  2018-10-31  3
2018    10  43  0   4
2018    10  43  0   5
2018    10  43  0   6
2018    11  42  0   7
2018    11  43  0   1
2018    11  43  0   2
2018    11  43  0   3
2018    11  43  2018-11-01  4
2018    11  43  2018-11-02  5
2018    11  43  2018-11-03  6
2018    11  43  2018-11-04  7
2018    11  44  2018-11-05  1
2018    11  44  2018-11-06  2
2018    11  44  2018-11-07  3
2018    11  44  2018-11-08  4
2018    11  44  2018-11-09  5
2018    11  44  2018-11-10  6
2018    11  44  2018-11-11  7
2018    11  45  2018-11-12  1
2018    11  45  2018-11-13  2
2018    11  45  2018-11-14  3
2018    11  45  2018-11-15  4
2018    11  45  2018-11-16  5
2018    11  45  2018-11-17  6
2018    11  45  2018-11-18  7
2018    11  46  2018-11-19  1
2018    11  46  2018-11-20  2
2018    11  46  2018-11-21  3
2018    11  46  2018-11-22  4
2018    11  46  2018-11-23  5
2018    11  46  2018-11-24  6
2018    11  46  2018-11-25  7
2018    11  47  2018-11-26  1
2018    11  47  2018-11-27  2
2018    11  47  2018-11-28  3
2018    11  47  2018-11-29  4
2018    11  47  2018-11-30  5
2018    11  47  0   6
2018    12  46  0   7
2018    12  47  0   1
2018    12  47  0   2
2018    12  47  0   3
2018    12  47  0   4
2018    12  47  0   5
2018    12  47  2018-12-01  6
2018    12  47  2018-12-02  7
2018    12  48  2018-12-03  1
2018    12  48  2018-12-04  2
2018    12  48  2018-12-05  3
2018    12  48  2018-12-06  4
2018    12  48  2018-12-07  5
2018    12  48  2018-12-08  6
2018    12  48  2018-12-09  7
2018    12  49  2018-12-10  1
2018    12  49  2018-12-11  2
2018    12  49  2018-12-12  3
2018    12  49  2018-12-13  4
2018    12  49  2018-12-14  5
2018    12  49  2018-12-15  6
2018    12  49  2018-12-16  7
2018    12  50  2018-12-17  1
2018    12  50  2018-12-18  2
2018    12  50  2018-12-19  3
2018    12  50  2018-12-20  4
2018    12  50  2018-12-21  5
2018    12  50  2018-12-22  6
2018    12  50  2018-12-23  7
2018    12  51  2018-12-24  1
2018    12  51  2018-12-25  2
2018    12  51  2018-12-26  3
2018    12  51  2018-12-27  4
2018    12  51  2018-12-28  5
2018    12  51  2018-12-29  6
2018    12  51  2018-12-30  7
2018    12  52  2018-12-31  1
2018    12  52  0   2
2018    12  52  0   3
2018    12  52  0   4
2018    12  52  0   5
2018    12  52  0   6

- 支持SQL Schema

CREATE TABLE `date_48361641` (
  `date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    truncate date_48361641 ;

insert into date_48361641 ( `date` )
select * from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2016-01-01' and '2018-12-31' ;

答案 2 :(得分:0)

此查询似乎可以解决您的查询。我假设您的最小日期超过1900-01-01。如果没有将任何其他日期低于您的最小日期并且是星期一 使用递归cte

也可能有更好的解决方案
with cte as (
    select cast(a as datetime) date, b week_day from (values ('20180201',4)
    ,('20180202',5)
    ,('20180203',6)
    ,('20180204',7)
    ,('20180205',1)
    ,('20180206',2)
    ,('20180207',3)
    ,('20180226',1)
    ,('20180227',2)
    ,('20180228',3) 

    ,('20181231',1)
    ,('20190101',2)
    ,('20190102',3)
    ,('20190103',4)
    ,('20190104',5)
    ) t(a,b)
)
select
    iif((rn = 1 and charindex(k, st) > 0) or k is null, convert(varchar(10), date, 120), '0')
    , isnull(k, week_day)
from (
    select
        date, week_day, ww
        , concat(max(iif(week_day = 1, 1, null)) over (partition by ww)
        , max(iif(week_day = 2, 2, null)) over (partition by ww)
        , max(iif(week_day = 3, 3, null)) over (partition by ww)
        , max(iif(week_day = 4, 4, null)) over (partition by ww)
        , max(iif(week_day = 5, 5, null)) over (partition by ww)
        , max(iif(week_day = 6, 6, null)) over (partition by ww)
        , max(iif(week_day = 7, 7, null)) over (partition by ww)) st
        , row_number() over (partition by ww order by week_day) rn
    from
        cte
        cross apply (select datediff(dd, cast('19000101' as datetime), date) / 7 ww) q
) t
left join (values ('1'),('2'),('3'),('4'),('5'),('6'),('7')) z(k) 
on 
    rn = 1 
    and (charindex(k, st) = 0 or iif(rn = 1, week_day, 0) = k)

答案 3 :(得分:-1)

这样的事情应该有效

with allDates as (
see link below to finish this part
)

select DateFieldFromYourTable
, isnull(WeekDay, 0) DayOfWeek
from allDates left join YourTable on allDates.Something = DateFieldFromYourTable

阅读this以获取子查询的代码。