How to Update Week Number in SQL? - sql

I have a following which are displayed order by date. I want to group each weekseries as below.
Select tq.ID,
CONVERT(VARCHAR(10),tq.DateCreated,101)WeekDate,
DATENAME(WEEKDAY,tq.DateCreated)WeekDays,
CASE When DATEPART(WEEKDAY,tq.DateCreated)-1=0 THEN 7 ELSE DATEPART(WEEKDAY,tq.DateCreated)-1 END as WeekSerial
From #temp tq
Current Data:
ID WeekDate WeekDays WeekSerial WeekNumber
56 2012-03-01 00:00:00.000 Thursday 4 NULL
57 2012-03-02 00:00:00.000 Friday 5 NULL
58 2012-03-03 00:00:00.000 Saturday 6 NULL
59 2012-03-04 00:00:00.000 Sunday 7 NULL
62 2012-03-05 00:00:00.000 Monday 1 NULL
63 2012-03-06 00:00:00.000 Tuesday 2 NULL
64 2012-03-07 00:00:00.000 Wednesday 3 NULL
65 2012-03-08 00:00:00.000 Thursday 4 NULL
67 2012-03-09 00:00:00.000 Friday 5 NULL
68 2012-03-10 00:00:00.000 Saturday 6 NULL
69 2012-03-11 00:00:00.000 Sunday 7 NULL
70 2012-03-12 00:00:00.000 Monday 1 NULL
71 2012-03-13 00:00:00.000 Tuesday 2 NULL
73 2012-03-14 00:00:00.000 Wednesday 3 NULL
74 2012-03-15 00:00:00.000 Thursday 4 NULL
76 2012-03-16 00:00:00.000 Friday 5 NULL
77 2012-03-17 00:00:00.000 Saturday 6 NULL
78 2012-03-18 00:00:00.000 Sunday 7 NULL
Required Data:
ID WeekDate WeekDays WeekSerial WeekNumber
56 2012-03-01 00:00:00.000 Thursday 4 1
57 2012-03-02 00:00:00.000 Friday 5 1
58 2012-03-03 00:00:00.000 Saturday 6 1
59 2012-03-04 00:00:00.000 Sunday 7 1
62 2012-03-05 00:00:00.000 Monday 1 2
63 2012-03-06 00:00:00.000 Tuesday 2 2
64 2012-03-07 00:00:00.000 Wednesday 3 2
65 2012-03-08 00:00:00.000 Thursday 4 2
67 2012-03-09 00:00:00.000 Friday 5 2
68 2012-03-10 00:00:00.000 Saturday 6 2
69 2012-03-11 00:00:00.000 Sunday 7 2
70 2012-03-12 00:00:00.000 Monday 1 3
71 2012-03-13 00:00:00.000 Tuesday 2 3
73 2012-03-14 00:00:00.000 Wednesday 3 3
74 2012-03-15 00:00:00.000 Thursday 4 3
76 2012-03-16 00:00:00.000 Friday 5 3
77 2012-03-17 00:00:00.000 Saturday 6 3
78 2012-03-18 00:00:00.000 Sunday 7 3
So, I want to group these values under weeknumber which must start from 1 for WeekSerial number ranges from 1 to 7.
NOTE: The week day starts from Monday to Sunday so its numbered from 1 through 7. i.e 1=Monday, 2=Tuesday and so on...!
Update:
INSERT INTO #Temp(KioskCount,KioskAmount,KioskAverage,WeekDate,WeekDays,WeekSerial)
Select COUNT(tq.quoteid)KioskCount,
SUM(tq.PriceQuote) [KioskAmount],
SUM(tq.PriceQuote) / COUNT(tq.QuoteID) [KioskAverage],
CONVERT(VARCHAR(10),tq.DateCreated,101)WeekDate,
DATENAME(WEEKDAY,tq.DateCreated)WeekDays,
CASE When DATEPART(WEEKDAY,tq.DateCreated)-1=0 THEN 7 ELSE DATEPART(WEEKDAY,tq.DateCreated)-1 END as WeekSerial
from tbl_Quotes tq
where
tq.QuoteStatusID <> 12 --remove void transactions
group by CONVERT(VARCHAR(10),tq.DateCreated,101),DATENAME(WEEKDAY,tq.DateCreated),DATEPART(WEEKDAY,tq.DateCreated)-1
order by 4

It is unclear actually what did you expect
You have already a WeekDate filed i think the date are correct and based on that you are filled your weekday field
then simply you can loop through the dates and update your week number using the WeekDate
i can provide an example with that
DECLARE #DATE DATETIME
DECLARE #count int
DECLARE #i int
SET #i=56// you can find your first id via query too
SET #count=(select COUNT(WeekDays) from tablename)
WHILE #i<=#count
BEGIN
SET #DATE =(select WeekDate from tablename where ID=#i)
update tablename set WeekNumber=(
SELECT DATEPART(WEEK, #DATE) -
DATEPART(WEEK, DATEADD(MM, DATEDIFF(MM,0,#DATE), 0))+ 1 AS WEEK_OF_MONTH)
where ID=#i
set #i=#i + 1
END;
it will update all your weeknumber field with corresponding number

Probably this helps you:
First you need to check the first of the day in your database
SELECT ##DATEFIRST
By default SUNDAY is first day of week (US). So you need to change it to 1 (Monday)
SET DATEFIRST 1
More inforamtion about SET DATEFIRST
In SQL Server by using DATEPART built-in function you can get the number of week in a year
SELECT DATAPART(WEEK, WeekDate)
Also you can get YEAR of your selected datetime
SELECT DATAPART(YEAR, WeekDate)
If you run these two queries for your first row (Let's say Week number 1)
SELECT DATEPART(WEEK, '2012-03-01 00:00:00.000') -- Output = 10
SELECT DATEPART(YEAR, '2012-03-01 00:00:00.000') -- Output = 2012
But this week should be your week number 1.
So you can easily subtract 2012 from your YEAR then multiply it by 52 (because we have 52 weeks in a year)
Substract 9 from your WEEKNUMBER
Add the numbers you get from above
(YEAR - 2012) * 52 + (WEEK - 9)
So by running this you can get the actual result
SELECT (DATEPART(YEAR, WeekDate) - 2012) * 52 + DATEPART(WEEK, WeekDate) - 9 AS WeekNumber
FROM yourTable

Related

How to get last N week data in different year

I need to get last 6 weeks data from some table, right now the logic that I use is this
WEEK([date column]) BETWEEN WEEK(NOW()) - 6 AND WEEK(NOW())
It run as I want, but January is near and I realize that this query will not working as it is. I try to run my query on 15th January 2022, I only get data from 1st January to 15th January when I use my logic.
TGL MINGGU_KE
2022-01-01 | 1
2022-01-02 | 2
2022-01-03 | 2
2022-01-04 | 2
2022-01-05 | 2
2022-01-06 | 2
2022-01-07 | 2
2022-01-08 | 2
2022-01-09 | 3
2022-01-10 | 3
2022-01-11 | 3
2022-01-12 | 3
2022-01-13 | 3
2022-01-14 | 3
2022-01-15 | 3
Can I get the last 6 weeks data including last year?
This is my dbfiddle: https://dbfiddle.uk/o9BeAFJF
You can round the dates to the first day of the week using ROUND, TRUNC or THIS_WEEK
WITH
SEARCH_WEEK (TGL) AS (
VALUES date '2020-12-01'
UNION ALL
SELECT tgl + 1 DAY FROM SEARCH_WEEK WHERE tgl < CURRENT date
),
BASE_DATE (base_date) AS (
VALUES date '2022-01-15'
),
OPTIONS (OPTION, OPTION_BASE_DATE) AS (
SELECT OPTION, option_base_date FROM base_date CROSS JOIN LATERAL (
VALUES
('ROUND D', ROUND(base_date, 'D')),
('ROUND IW', ROUND(base_date, 'IW')),
('ROUND W', ROUND(base_date, 'W')),
('ROUND WW', ROUND(base_date, 'WW')),
('TRUNC D', TRUNC(base_date, 'D')),
('TRUNC IW', TRUNC(base_date, 'IW')),
('TRUNC W', TRUNC(base_date, 'W')),
('TRUNC WW', TRUNC(base_date, 'WW')),
('THIS_WEEK', THIS_WEEK(base_date)),
('THIS_WEEK + 1 DAY', THIS_WEEK(base_date) + 1 DAY)
) a (OPTION, OPTION_BASE_DATE)
)
SELECT
OPTION,
MIN(TGL) BEGIN,
max(tgl) END,
dayname(MIN(TGL)) day_BEGIN,
dayname(max(tgl)) day_end,
days_between(max(tgl), min(tgl)) + 1 duration_in_days
FROM
SEARCH_WEEK
CROSS JOIN options
WHERE
TGL BETWEEN option_base_date - 35 DAYS AND option_base_date + 6 DAYS
GROUP BY OPTION
OPTION
BEGIN
END
DAY_BEGIN
DAY_END
DURATION_IN_DAYS
ROUND D
2021-12-12
2022-01-22
Sunday
Saturday
42
ROUND IW
2021-12-13
2022-01-23
Monday
Sunday
42
ROUND W
2021-12-11
2022-01-21
Saturday
Friday
42
ROUND WW
2021-12-11
2022-01-21
Saturday
Friday
42
THIS_WEEK
2021-12-05
2022-01-15
Sunday
Saturday
42
THIS_WEEK + 1 DAY
2021-12-06
2022-01-16
Monday
Sunday
42
TRUNC D
2021-12-05
2022-01-15
Sunday
Saturday
42
TRUNC IW
2021-12-06
2022-01-16
Monday
Sunday
42
TRUNC W
2021-12-11
2022-01-21
Saturday
Friday
42
TRUNC WW
2021-12-11
2022-01-21
Saturday
Friday
42
fiddle
you can use dateadd to get first day of week six weeks ago like this:
Select * from tableName
where [dateColumn] between dateadd(WEEK,-6,getdate()) and getdate()
You can use DATEADD to get last 6 weeks of data as follows:
Select * from [TableName] where [DateColumn] between
DATEADD(WEEK,-6,GETDATE()) and GETDATE();

How to use next date column value to calculate delta for current column

I have a temp table
BusinessDate SSQ_CompScore
2011-01-05 00:00:00.000 41
2011-01-06 00:00:00.000 6
2011-01-07 00:00:00.000 1
2011-01-10 00:00:00.000 8
2011-01-11 00:00:00.000 48
2011-01-12 00:00:00.000 50
2011-01-13 00:00:00.000 59
I need to calculate delta for each current date.
I have prepared a solution but it doesn't work where date as not consecutive.
Can you please help?
select t1.businessdate, t1.ssq_compscore, (t2.ssq_compscore - t1.ssq_compscore) as delta
from #temp t1
left join #temp t2 on t1.businessdate = DATEADD(dd,1,t2.businessdate)
where t1.businessdate >='20180814'
Result set should be as
BusinessDate SSQ_CompScore Delta
2011-01-05 00:00:00.000 41 NULL
2011-01-06 00:00:00.000 6 35
2011-01-07 00:00:00.000 1 5
2011-01-10 00:00:00.000 8 7
2011-01-11 00:00:00.000 48 40
2011-01-12 00:00:00.000 50 2
2011-01-13 00:00:00.000 59 9
Not sure if this is the most efficient way but it works as far as I see
SELECT businessdate, SSQ_CompScore ,
SSQ_CompScore - (SELECT SSQ_CompScore
FROM temp
WHERE businessdate < t1.businessdate
ORDER BY businessdate DESC
LIMIT 1) as delta
FROM temp t1
ORDER BY businessdate ASC

Skip Holidays in Business day Table

I am using the following script to determine what the business days are for each particular month.
DECLARE #startdate DATETIME
SET #startdate ='20170401'
;
WITH bd AS(
SELECT
DATEADD(DAY,
CASE
(DATEPART(WEEKDAY, DATEADD(MONTH, DATEDIFF(MONTH, 0, #startdate), 0)) + ##DATEFIRST - 1) % 7
WHEN 6 THEN 2
WHEN 7 THEN 1
ELSE 0
END,
DATEADD(MONTH, DATEDIFF(MONTH, 0, #startdate), 0)
) AS bd, 1 AS n
UNION ALL
SELECT DATEADD(DAY,
CASE
(DATEPART(WEEKDAY, bd.bd) + ##DATEFIRST - 1) % 7
WHEN 5 THEN 3
WHEN 6 THEN 2
ELSE 1
END,
bd.bd
) AS db,
bd.n+1
FROM bd WHERE MONTH(bd.bd) = MONTH(#startdate)
)
SELECT * INTO #BD
FROM (
SELECT 'BD'+ CAST(n AS VARCHAR(5)) AS Expected_Date_Rule, bd AS Expected_Calendar_Date
from bd
) AS x
The result of this table works fine. Bd is the the calendar days for the particular month and n is the business day number. The script does its job of not counting the weekend as a business day.
bd n
----------------------- -----------
2017-04-03 00:00:00.000 1
2017-04-04 00:00:00.000 2
2017-04-05 00:00:00.000 3
2017-04-06 00:00:00.000 4
2017-04-07 00:00:00.000 5
2017-04-10 00:00:00.000 6
2017-04-11 00:00:00.000 7
2017-04-12 00:00:00.000 8
2017-04-13 00:00:00.000 9
2017-04-14 00:00:00.000 10
2017-04-17 00:00:00.000 11
2017-04-18 00:00:00.000 12
2017-04-19 00:00:00.000 13
2017-04-20 00:00:00.000 14
2017-04-21 00:00:00.000 15
2017-04-24 00:00:00.000 16
2017-04-25 00:00:00.000 17
2017-04-26 00:00:00.000 18
2017-04-27 00:00:00.000 19
2017-04-28 00:00:00.000 20
2017-05-01 00:00:00.000 21
But then I notice that a potential issue will occur in July where the output will count the 4th of July as BD2 when it should be counted as BD3. Some had created a holiday table that is updated with all the holidays (excuse the bad spelling).
Holiday table
1 2017-01-01 New Year Day
4 2017-01-02 New Year Day-Follow
1 2017-01-16 MArtin Luther King Day
4 2017-01-17 MArtin Luther King Day-Follow
1 2017-02-20 Preseiednt Day
4 2017-02-21 Preseiednt Day-Follow
1 2017-05-29 Memorial Day
4 2017-05-30 Memorial Day-Follow
1 2017-07-04 Independence Day
4 2017-07-05 Independence Day-Follow
1 2017-09-04 Labour Day
4 2017-09-05 Labour Day-Follow
1 2017-10-09 Columbus Day
4 2017-10-10 Columbus Day-Follow
1 2017-11-10 Vetrans Day
4 2017-11-11 Vetrans Day-Follow
1 2017-11-23 ThanksGiving
1 2017-11-24 Day After Thanks Giving
4 2017-11-24 ThanksGiving-Follow
4 2017-11-25 Day After Thanks Giving-Follow
1 2017-12-25 Christmas
4 2017-12-26 Christmas-Follow
I was thinking there may be some way I can update my script to check the holiday table and skip the holiday and dont count it as a business day. Any tips?

Monthly weights on a daily grain

I would like to write a tsql script that would display five columns. First column is date, second is the day of the week, third is how many of that day in a week exist in the current month, fourth is weight of the day and the fifth is the weight of the month (Mon through Wed get 15 each and Thu through Sun get 10 each). What's the easiest and fastest way to achieve that?
Example output:
Date Day_of_the_week Occurs_in_a_month Weight_of_the_day Weight_of_the_month
1/1/2015 Thursday 5 10 370
1/2/2015 Friday 5 10 370
1/3/2015 Saturday 5 10 370
1/4/2015 Sunday 4 10 370
1/5/2015 Monday 4 15 370
1/6/2015 Tuesday 4 15 370
1/7/2015 Wednesday 4 15 370
1/8/2015 Thursday 5 10 370
1/9/2015 Friday 5 10 370
1/10/2015 Saturday 5 10 370
1/11/2015 Sunday 4 10 370
1/12/2015 Monday 4 15 370
1/13/2015 Tuesday 4 15 370
1/14/2015 Wednesday 4 15 370
1/15/2015 Thursday 5 10 370
1/16/2015 Friday 5 10 370
1/17/2015 Saturday 5 10 370
1/18/2015 Sunday 4 10 370
1/19/2015 Monday 4 15 370
1/20/2015 Tuesday 4 15 370
1/21/2015 Wednesday 4 15 370
1/22/2015 Thursday 5 10 370
1/23/2015 Friday 5 10 370
1/24/2015 Saturday 5 10 370
1/25/2015 Sunday 4 10 370
1/26/2015 Monday 4 15 370
1/27/2015 Tuesday 4 15 370
1/28/2015 Wednesday 4 15 370
1/29/2015 Thursday 5 10 370
1/30/2015 Friday 5 10 370
1/31/2015 Saturday 5 10 370
2/1/2015 Sunday 4 10 340
2/2/2015 Monday 4 15 340
2/3/2015 Tuesday 4 15 340
2/4/2015 Wednesday 4 15 340
2/5/2015 Thursday 4 10 340
2/6/2015 Friday 4 10 340
2/7/2015 Saturday 4 10 340
2/8/2015 Sunday 4 10 340
2/9/2015 Monday 4 15 340
2/10/2015 Tuesday 4 15 340
2/11/2015 Wednesday 4 15 340
2/12/2015 Thursday 4 10 340
2/13/2015 Friday 4 10 340
2/14/2015 Saturday 4 10 340
2/15/2015 Sunday 4 10 340
2/16/2015 Monday 4 15 340
2/17/2015 Tuesday 4 15 340
2/18/2015 Wednesday 4 15 340
2/19/2015 Thursday 4 10 340
2/20/2015 Friday 4 10 340
2/21/2015 Saturday 4 10 340
2/22/2015 Sunday 4 10 340
2/23/2015 Monday 4 15 340
2/24/2015 Tuesday 4 15 340
2/25/2015 Wednesday 4 15 340
2/26/2015 Thursday 4 10 340
2/27/2015 Friday 4 10 340
2/28/2015 Saturday 4 10 340
.
.
.
.
Say between the years of 2015 and 2020.
Use recursive cte to build the date list.. the rest is just window functions
WITH cteDates AS (
SELECT CONVERT(DATE, '2015-01-01') AS [Date]
UNION ALL
SELECT DATEADD(DAY, 1, [Date])
FROM cteDates
WHERE DATEADD(DAY, 1, [Date]) < '2021-01-01'
),
cteWeights AS (
SELECT [Date],
DATENAME(weekday, [Date]) [Day_of_the_week],
COUNT(*) OVER (PARTITION BY DATEPART(MONTH, [Date]), DATEPART(YEAR, [Date]),DATENAME(weekday, [Date])) [Occurs_in_a_month],
(CASE WHEN DATENAME(weekday, [Date]) IN ('Monday', 'Tuesday', 'Wednesday') THEN 15 ELSE 10 END) [Weight_of_the_day]
FROM cteDates
)
SELECT *,
SUM([Weight_of_the_day]) OVER (PARTITION BY DATEPART(MONTH, [Date]), DATEPART(YEAR, [Date])) [ Weight_of_the_month]
FROM cteWeights
ORDER BY [Date]
OPTION (MAXRECURSION 0)

how to get data until the next 1 number

I have a date table where I have assinged business days. I need to get business from, for example, 1-20 before the next number 1. How can I do that? Here is a smaller verion of my table:
Date Day BusinessDays
2015-05-01 Friday 1
2015-05-02 Saturday 2
2015-05-03 Sunday 2
2015-05-04 Monday 2
2015-05-05 Tuesday 3
2015-05-06 Wednesday 4
2015-05-07 Thursday 5
2015-05-08 Friday 6
2015-05-09 Saturday 7
2015-05-10 Sunday 7
2015-05-11 Monday 7
2015-05-12 Tuesday 8
2015-05-13 Wednesday 9
2015-05-14 Thursday 10
2015-05-15 Friday 11
2015-05-16 Saturday 12
2015-05-17 Sunday 12
2015-05-18 Monday 12
2015-05-19 Tuesday 13
2015-05-20 Wednesday 14
2015-05-21 Thursday 15
2015-05-22 Friday 16
2015-05-23 Saturday 17
2015-05-24 Sunday 17
2015-05-25 Monday 17
2015-05-26 Tuesday 17
2015-05-27 Wednesday 18
2015-05-28 Thursday 19
2015-05-29 Friday 20
*2015-05-30 Saturday 1
*2015-05-31 Sunday 1
*2015-06-01 Monday 1
*2015-06-02 Tuesday 2
*2015-06-03 Wednesday 3
I need to get data from 1 to 20 business days and don't include the numbers that starts again from one (for example exclude rows that have * in front). This needs to be dynamic. Since DayName will change for every number so I can't include that in my where clause.
Let me assume that you have a date in mind, so you want everything from that date to the next "1".
select t.*
from datetable t
where t.date >= '2015-05-01' and
t.date < (select min(t2.date)
from datetable t2
where t2.date > '2015-05-01' and
t2.businessdays = 1
);