Monthly weights on a daily grain - sql

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)

Related

Filter rows of a table based on a condition that implies: 1) value of a field within a range 2) id of the business and 3) date?

I want to filter a TableA, taking into account only those rows whose "TotalInvoice" field is within the minimum and maximum values expressed in a ViewB, based on month and year values and RepairShopId (the sample data only has one RepairShopId, but all the data has multiple IDs).
In the view I have minimum and maximum values for each business and each month and year.
TableA
RepairOrderDataId
RepairShopId
LastUpdated
TotalInvoice
1
10
2017-06-01 07:00:00.000
765
1
10
2017-06-05 12:15:00.000
765
2
10
2017-02-25 13:00:00.000
400
3
10
2017-10-19 12:15:00.000
295679
4
10
2016-11-29 11:00:00.000
133409.41
5
10
2016-10-28 12:30:00.000
127769
6
10
2016-11-25 16:15:00.000
122400
7
10
2016-10-18 11:15:00.000
1950
8
10
2016-11-07 16:45:00.000
79342.7
9
10
2016-11-25 19:15:00.000
1950
10
10
2016-12-09 14:00:00.000
111559
11
10
2016-11-28 10:30:00.000
106333
12
10
2016-12-13 18:00:00.000
23847.4
13
10
2016-11-01 17:00:00.000
22782.9
14
10
2016-10-07 15:30:00.000
NULL
15
10
2017-01-06 15:30:00.000
138958
16
10
2017-01-31 13:00:00.000
244484
17
10
2016-12-05 09:30:00.000
180236
18
10
2017-02-14 18:30:00.000
92752.6
19
10
2016-10-05 08:30:00.000
161952
20
10
2016-10-05 08:30:00.000
8713.08
ViewB
RepairShopId
Orders
Average
MinimumValue
MaximumValue
year
month
yearMonth
10
1
370343
370343
370343
2015
7
2015-7
10
1
109645
109645
109645
2015
10
2015-10
10
1
148487
148487
148487
2015
12
2015-12
10
1
133409.41
133409.41
133409.41
2016
3
2016-3
10
1
19261
19261
19261
2016
8
2016-8
10
4
10477.3575
2656.65644879821
18298.0585512018
2016
9
2016-9
10
69
15047.709565
10
90942.6052417394
2016
10
2016-10
10
98
22312.077244
10
147265.581935242
2016
11
2016-11
10
96
20068.147395
10
99974.1750708773
2016
12
2016-12
10
86
25334.053372
10
184186.985160105
2017
1
2017-1
10
69
21410.63855
10
153417.00126689
2017
2
2017-2
10
100
13009.797
10
59002.3589332934
2017
3
2017-3
10
101
11746.191287
10
71405.3391452842
2017
4
2017-4
10
123
11143.49756
10
55306.8202091131
2017
5
2017-5
10
197
15980.55406
10
204538.144334771
2017
6
2017-6
10
99
10852.496969
10
63283.9899761938
2017
7
2017-7
10
131
52601.981526
10
1314998.61355187
2017
8
2017-8
10
124
10983.221854
10
59444.0535811233
2017
9
2017-9
10
115
12467.148434
10
72996.6054527277
2017
10
2017-10
10
123
14843.379593
10
129673.931373139
2017
11
2017-11
10
111
8535.455945
10
50328.1495501884
2017
12
2017-12
I've tried:
SELECT *
FROM TableA
INNER JOIN ViewB ON TableA.RepairShopId = ViewB.RepairShopId
WHERE TotalInvoice > MinimumValue AND TotalInvoice < MaximumValue
AND TableA.RepairShopId = ViewB.RepairShopId
But I'm not sure how to compare it the yearMonth field with the datetime field "LastUpdated".
Any help is very appreciated!
here is how you can do it:
I assumed LastUpdated column is the column from tableA which indicate date of
SELECT *
FROM TableA A
INNER JOIN ViewB B
ON A.RepairShopId = B.RepairShopId
AND A.TotalInvoice > B.MinimumValue
AND A.TotalInvoice < B.MaximumValue
AND YEAR(LastUpdated) = B.year
AND MONTH(LastUpdated) = B.month

Grouping into series based on days since

I need to create a new grouping every time I have a period of more than 60 days since my previous record.
Basically, I need too take the data I have here:
RowNo StartDate StopDate DaysBetween
1 3/21/2017 3/21/2017 14
2 4/4/2017 4/4/2017 14
3 4/18/2017 4/18/2017 14
4 6/23/2017 6/23/2017 66
5 7/5/2017 7/5/2017 12
6 7/19/2017 7/19/2017 14
7 9/27/2017 9/27/2017 70
8 10/24/2017 10/24/2017 27
9 10/31/2017 10/31/2017 7
10 11/14/2017 11/14/2017 14
And turn it into this:
RowNo StartDate StopDate DaysBetween Series
1 3/21/2017 3/21/2017 14 1
2 4/4/2017 4/4/2017 14 1
3 4/18/2017 4/18/2017 14 1
4 6/23/2017 6/23/2017 66 2
5 7/5/2017 7/5/2017 12 2
6 7/19/2017 7/19/2017 14 2
7 9/27/2017 9/27/2017 70 3
8 10/24/2017 10/24/2017 27 3
9 10/31/2017 10/31/2017 7 3
10 11/14/2017 11/14/2017 14 3
Once I have that I'll group by Series and get the min(StartDate) and max(StopDate) for individual durations.
I could do this using a cursor but I'm sure someone much smarter than me has figured out a more elegant solution. Thanks in advance!
You can use the window function sum() over with a conditional FLAG
Example
Select *
,Series= 1+sum(case when [DaysBetween]>60 then 1 else 0 end) over (Order by RowNo)
From YourTable
Returns
RowNo StartDate StopDate DaysBetween Series
1 2017-03-21 2017-03-21 14 1
2 2017-04-04 2017-04-04 14 1
3 2017-04-18 2017-04-18 14 1
4 2017-06-23 2017-06-23 66 2
5 2017-07-05 2017-07-05 12 2
6 2017-07-19 2017-07-19 14 2
7 2017-09-27 2017-09-27 70 3
8 2017-10-24 2017-10-24 27 3
9 2017-10-31 2017-10-31 7 3
10 2017-11-14 2017-11-14 14 3
EDIT - 2008 Version
Select A.*
,B.*
From YourTable A
Cross Apply (
Select Series=1+sum( case when [DaysBetween]>60 then 1 else 0 end)
From YourTable
Where RowNo <= A.RowNo
) B

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?

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
);

How to Update Week Number in 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