简体   繁体   中英

Incremental Week Number in SQL Server

I am trying to increase week number so that the week number of the year will not repeat

For example 18/12/2016 is week number 51, 25/12/2016 is week number 52 and 01/01/2017 is week number 1, 08/01/2017 is week number 2.

What I want is 18/12/2016 week NO 51, 25/12/2016 week No 52, week No 53, 08/01/2017 No week 54 etc so that no week will repeat

See the sample of my dataset and query I have tried

DECLARE @Table1 table (Week_End_Date DATE)

INSERT INTO @Table1 
VALUES ('2016-04-03'), ('2016-04-10'), ('2016-04-17'), ('2016-04-24'),
       ('2016-05-01'), ('2016-05-08'), ('2016-05-15'), ('2016-05-22'),
       ('2016-05-29'), ('2016-06-05'), ('2016-06-12'), ('2016-06-19'),
       ('2016-06-26'), ('2016-07-03'), ('2016-07-10'), ('2016-07-17'),
       ('2016-07-24'), ('2016-07-31'), ('2016-08-07'), ('2016-08-14'),
       ('2016-08-21'), ('2016-08-28'), ('2016-09-04'), ('2016-09-11'),
       ('2016-09-18'), ('2016-09-25'), ('2016-10-02'), ('2016-10-09'),   
       ('2016-10-16'), ('2016-10-23'), ('2016-10-30'), ('2016-11-06'),
       ('2016-11-13'), ('2016-11-20'), ('2016-11-27'), ('2016-12-04'),
       ('2016-12-11'), ('2016-12-18'), ('2016-12-25'), ('2017-01-01'),
       ('2017-01-08'), ('2017-01-15'), ('2017-01-22'), ('2017-01-29'),   
       ('2017-02-05'), ('2017-02-12'), ('2017-02-19'), ('2017-02-26'),
       ('2017-03-05'), ('2017-03-12'), ('2017-03-19'), ('2017-03-26'),
       ('2017-04-02'), ('2017-04-09'), ('2017-04-16'), ('2017-04-23'),
       ('2017-04-30'), ('2017-05-07'), ('2017-05-14'), ('2017-05-21'),
       ('2017-05-28'), ('2017-06-04'), ('2017-06-11'), ('2017-06-18'),
       ('2017-06-25'), ('2017-07-02'), ('2017-07-09'), ('2017-07-16'),
       ('2017-07-23'), ('2017-07-30'), ('2017-08-06'), ('2017-08-13'),
       ('2017-08-20'), ('2017-08-27'), ('2017-09-03'), ('2017-09-10'),
       ('2017-09-17'), ('2017-09-24'), ('2017-10-01'), ('2017-10-08'),
       ('2017-10-15'), ('2017-10-22'), ('2017-10-29'), ('2017-11-05'),
       ('2017-11-12'), ('2017-11-19'), ('2017-11-26'), ('2017-12-03'),
       ('2017-12-10'), ('2017-12-17'), ('2017-12-24'), ('2017-12-31'),
       ('2018-01-07'), ('2018-01-14'), ('2018-01-21'), ('2018-01-28'),
       ('2018-02-04'), ('2018-02-11'), ('2018-02-18'), ('2018-02-25'),
       ('2018-03-04'), ('2018-03-11'), ('2018-03-18'), ('2018-03-25'),
       ('2018-04-01')

Query:

SELECT
    Week_End_Date,
    DATEPART(WEEK,Week_End_Date) AS WeekNumber,
    REPLACE(LEFT(Week_End_Date,7),'-','') +
         CASE 
            WHEN CAST(DATEPART(WEEK, Week_End_Date) AS VARCHAR(2)) IN ('1', '2', '3', '4', '5', '6', '7', '8', '9') 
               THEN '0' + CAST(DATEPART(WEEK, Week_End_Date) AS VARCHAR(2)) 
               ELSE CAST(DATEPART(WEEK, Week_End_Date) AS VARCHAR(2))
         END Wk_NO_Norepeat
FROM 
    @Table1 
ORDER BY 
    Week_End_Date

Desired output

Date    Current week number Expected output
04/12/2016  49  49
11/12/2016  50  50
18/12/2016  51  51
25/12/2016  52  52
01/01/2017  1   53
08/01/2017  2   54
15/01/2017  3   55
22/01/2017  4   56
29/01/2017  5   57

Perhaps this does what you want:

select t1.weekenddate, datepart(week, t1.weekenddate) AS WeekNumber,
       (first_value(datepart(week, t1.weekenddate)) over (order by t1.weekenddate) +
        row_number() over (order by t1.weekenddate) - 1
       ) as newweeknumber
from @table1 t1;

It calculates the first week number in the data and then just increments the week number by 1 in subsequent rows.

You could use ROW_NUMBER(). If i understand it correctly you may use something like:

DECLARE @Table1 table (Week_End_Date DATE)

INSERT INTO @Table1 
VALUES ('2016-04-03'), ('2016-04-10'), ('2016-04-17'), ('2016-04-24'),
       ('2016-05-01'), ('2016-05-08'), ('2016-05-15'), ('2016-05-22'),
       ('2016-05-29'), ('2016-06-05'), ('2016-06-12'), ('2016-06-19'),
       ('2016-06-26'), ('2016-07-03'), ('2016-07-10'), ('2016-07-17'),
       ('2016-07-24'), ('2016-07-31'), ('2016-08-07'), ('2016-08-14'),
       ('2016-08-21'), ('2016-08-28'), ('2016-09-04'), ('2016-09-11'),
       ('2016-09-18'), ('2016-09-25'), ('2016-10-02'), ('2016-10-09'),   
       ('2016-10-16'), ('2016-10-23'), ('2016-10-30'), ('2016-11-06'),
       ('2016-11-13'), ('2016-11-20'), ('2016-11-27'), ('2016-12-04'),
       ('2016-12-11'), ('2016-12-18'), ('2016-12-25'), ('2017-01-01'),
       ('2017-01-08'), ('2017-01-15'), ('2017-01-22'), ('2017-01-29'),   
       ('2017-02-05'), ('2017-02-12'), ('2017-02-19'), ('2017-02-26'),
       ('2017-03-05'), ('2017-03-12'), ('2017-03-19'), ('2017-03-26'),
       ('2017-04-02'), ('2017-04-09'), ('2017-04-16'), ('2017-04-23'),
       ('2017-04-30'), ('2017-05-07'), ('2017-05-14'), ('2017-05-21'),
       ('2017-05-28'), ('2017-06-04'), ('2017-06-11'), ('2017-06-18'),
       ('2017-06-25'), ('2017-07-02'), ('2017-07-09'), ('2017-07-16'),
       ('2017-07-23'), ('2017-07-30'), ('2017-08-06'), ('2017-08-13'),
       ('2017-08-20'), ('2017-08-27'), ('2017-09-03'), ('2017-09-10'),
       ('2017-09-17'), ('2017-09-24'), ('2017-10-01'), ('2017-10-08'),
       ('2017-10-15'), ('2017-10-22'), ('2017-10-29'), ('2017-11-05'),
       ('2017-11-12'), ('2017-11-19'), ('2017-11-26'), ('2017-12-03'),
       ('2017-12-10'), ('2017-12-17'), ('2017-12-24'), ('2017-12-31'),
       ('2018-01-07'), ('2018-01-14'), ('2018-01-21'), ('2018-01-28'),
       ('2018-02-04'), ('2018-02-11'), ('2018-02-18'), ('2018-02-25'),
       ('2018-03-04'), ('2018-03-11'), ('2018-03-18'), ('2018-03-25'),
       ('2018-04-01')

DECLARE @FirstWeek INT = (SELECT DATEPART(WEEK,MIN(Week_End_Date))-1 FROM @Table1)

SELECT
    Week_End_Date,
    ROW_NUMBER() OVER (ORDER BY Week_End_Date)+@FirstWeek AS WeekNumber
FROM 
    @Table1 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM