week beginning (Monday) for previous day - sql

I want to pull all the data for the week to the previous day.
for example:
if the date was Monday 2016-05-09. I would like the previous Monday 2016-05-02 to Sunday 2016-05-08.
If the date was Tuesday 2016-05-10. I would like the previous Monday 2016-05-09 to Monday 2016-05-09.
I have tried using with no joy.
and (ShiftDate Between DATEADD(wk, 0, DATEADD(DAY, (1-DATEPART(WEEKDAY, GETDATE()-1)), DATEDIFF(dd, 0, GETDATE())))and GETDATE()-1)

You should use this:
and (ShiftDate Between (GETDATE() - 1 - (DATEPART(weekday, GETDATE() -1) + 7 - 2) % 7)
and (GETDATE()-1)
)
Because Sunday is 1 in weekday so you should add 7 then module by 7 to get the different from nearest Monday (2 in weekday) to the previous day.

Related

Getting week of the month. Week should start on Monday and end a Sunday

I need to add new business wee to my dim_date actually i have week of the month that is counting every 7 days of month as the table bellow.
Date
Week of month
day_of_week
01/04/2022
1
Friday
02/04/2022
1
Saturday
03/04/2022
1
Sunday
04/04/2022
1
Monday
05/04/2022
1
Tuesday
06/04/2022
1
Wednesday
07/04/2022
1
Thursday
08/04/2022
2
Friday
09/04/2022
2
Saturday
10/04/2022
2
Sunday
But for this business week column the week should start on Monday and end at Sunday, even if the first week is only one day(when a month starts at Sunday), but if the month starts on Friday, I should consider it as the first week of the month.
Date
Week of month
day_of_week
01/04/2022
1
Friday
02/04/2022
1
Saturday
03/04/2022
1
Sunday
04/04/2022
2
Monday
05/04/2022
2
Tuesday
06/04/2022
2
Wednesday
07/04/2022
2
Thursday
08/04/2022
2
Friday
09/04/2022
2
Saturday
10/04/2022
2
Sunday
Do you have any suggestion to calculate this?
Thank you
This query gets the number of the week from 2002-04-01 to 2002-04-19. The week start from monday and finishes on sunday. You can see this example:
with sample_data as (
SELECT date FROM UNNEST(generate_timestamp_array('2022-04-01', '2022-04-19', INTERVAL 1 DAY)) as date
)
select
date
, extract(week(MONDAY) from date)-(extract(MONTh from date)-1)*4 AS days_diff
, FORMAT_DATE('%A', date)
from sample_data
You can see the result.
You need to use EXTRACT function.
i hope that this solution i figured out could help you in the future or we can discuss and imporve this solution:
WITH sample_data AS (SELECT
date,
EXTRACT(WEEK FROM date) AS week_of_year,
DATE_TRUNC(date, MONTH) AS FIRST_DAY_OF_MONTH
FROM (SELECT date
FROM UNNEST(GENERATE_TIMESTAMP_ARRAY('2022-04-01', '2022-04-19', INTERVAL 1 DAY)) AS date))
SELECT
date,
FORMAT_DATE('%A', date) as day_name,
CASE
WHEN CAST(EXTRACT(WEEK(MONDAY) FROM d.date) AS INT) = 0 AND d.WEEK_OF_YEAR IN (52, 53) THEN 1
WHEN CAST(EXTRACT(WEEK(MONDAY) FROM d.FIRST_DAY_OF_MONTH) AS INT) = 0 THEN EXTRACT(WEEK(MONDAY) FROM d.date) + 1
ELSE (d.WEEK_OF_YEAR - CAST(EXTRACT(WEEK(MONDAY) FROM d.FIRST_DAY_OF_MONTH) AS INT)) + 1
END AS BUSINESS_WEEK_OF_MONTH
FROM sample_data d;

Find date for last four sundays

Namely what I need is:
The key over here is that it does change the dates every week, from last Sunday, to 2nd last Sunday and so on, so that it is dynamic.
Will appreciate if someone can help.
CASE
WHEN f.[STARTDATE] = last week Sunday (03/06) THEN '1 week ago'
WHEN f.[STARTDATE] = last 2 week Sunday (02/27) THEN '2 weeks ago'
WHEN f.[STARTDATE] = last 3 week Sunday (02/20) THEN '3 week ago'
WHEN f.[STARTDATE] = last 4 week Sunday (02/13) THEN '4 week ago'
END AS 'STARTDATE'
Thank you
First you need to get nearest Sunday before you can subtract 7, 14, 21 and 28 days from it. The simplest way is to check current day of week and calculate an offset: 0 for Sunday, 1 for Monday and so on:
with cte as (
select offsett = case datename(weekday, cast(current_timestamp as date))
when 'sunday' then 0
when 'monday' then 1
when 'tuesday' then 2
when 'wednesday' then 3
when 'thursday' then 4
when 'friday' then 5
when 'saturday' then 6
end
)
select case
when f.[startdate] = dateadd(day, -cte.offsett - 7, cast(current_timestamp as date)) then '1 week ago'
when f.[startdate] = dateadd(day, -cte.offsett - 14, cast(current_timestamp as date)) then '2 week ago'
when f.[startdate] = dateadd(day, -cte.offsett - 21, cast(current_timestamp as date)) then '1 week ago'
when f.[startdate] = dateadd(day, -cte.offsett - 28, cast(current_timestamp as date)) then '2 week ago'
end
from f
cross join cte

Getting “before two days” date sql server

I want to get two days before from current date and that date should not be Saturday and Sunday today is 1st July 2021 so i need to get data for 30th jun 2021 and 29th jun 2021 and that day should not be Saturday and Sunday.
Use a CASE expression:
SELECT CASE DATEPART(dw, GETDATE())
WHEN 2 THEN DATEADD(day, -3, GETDATE()) -- Monday becomes Friday
WHEN 3 THEN DATEADD(day, -4, GETDATE()) -- Tuesday becomes Friday
ELSE DATEADD(day, -2, GETDATE()) -- otherwise roll back 2 days
END;
The above answer assumes that server settings place Sunday as day 1, Monday as day 2, ..., and Saturday as day 7. Without this assumption my answer might be worthless.

Sum over defined Week limits

I have a rolling date with associated hours. I want to sum the hours over my defined 7-day week range of Saturday to Friday.
So I need to define any date as a week beginning Saturday, week ending Friday and sum over this range.
The table is in the form of:
Date
Day
Hours
2021-06-12
Saturday
3
2021-06-18
Friday
3
2021-06-21
Monday
1
2021-06-22
Tuesday
2
Grouping the above table into Saturday-Friday Week sums.
Saturday
Friday
Total_Hours
2021-06-12
2021-06-18
6
2021-06-19
2021-06-25
3
You can use date functions to get for each date (other than Saturday) the previous Saturday which is the start of the week it belongs too and group by that:
WITH cte AS (
SELECT *, DATEADD(day, -DATEPART(dw, Date) % 7, Date) Saturday
FROM tablename
)
SELECT Saturday,
DATEADD(day, 6, Saturday) Friday,
SUM(Hours) Total_Hours
FROM cte
GROUP BY Saturday
See the demo.
This code works in SQL Server but similar logic and functions can be used for other databases.
You don't have to tie this to datepart() or datename(), which in turn depends on DATEFIRST (alas!).
Instead, you can use date arithmetic. This in turn depends on the fact that 0 date is a Monday. But that is not configurable (as far as I know).
So:
select v.week as saturday, dateadd(day, 6, v.week) as friday,
sum(hours)
from t cross apply
(values (dateadd(day, 7*datediff(week, 0, dateadd(day, 1, t.date)) - 2, 0))
) v(week)
group by v.week
order by v.week;
Here is a db<>fiddle.

SQL: First day of week Monday

I would like to display Monday as the first day of the week and Sunday the last day of the week.
I am using for this report the Report server.
my query:
Select
Datum,
Sum(Prod) as Prod
FROM (
Select
intervaldate as Datum,
Sum(case when TabName = 'Produzierte Dosen' then DisplayUnits else 0 end) as Prod
from vwOeeIntervalCount
where
IntervalDateWeek >= dateadd(wk, datediff(wk, 0, getdate()) - 1, 0)
and IntervalDateWeek < dateadd(wk, datediff(wk, 0, getdate()), 0)
and IntervalDate >= dateadd(day,datediff(day,0,GETDATE())-6,0)
AND IntervalDate < dateadd(day,datediff(day,0,GETDATE()),0)
and CalculationName = 'Packaging'
group by intervaldate
)c
group by Datum
Here the result:
Date |Prod
2018-02-25 00:00:00.000 |1836528
2018-02-26 00:00:00.000 |8131127,99999999
EDIT:
Sorry here is my question.
I would like to display the Monday as the first day of the week. My query displays the Sunday as the first day of the week.
How can I do that?
By default MS SQL Server has configured sunday as first day of a week.
You can set the server config for the first day of a week to any day with the following command:
SET DATEFIRST { number | #number_var }
Value First day of the week is
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 (default, U.S. English) Sunday
Source: https://learn.microsoft.com/en-us/sql/t-sql/statements/set-datefirst-transact-sql
you can use this to the current week:
/*-- Week Start
--1->Sunday --2->Monday....*/
SELECT CAST(DATEADD(dd, -(DATEPART(dw, GETDATE()))+2, GETDATE()) AS DATE) [WeekStart],
CAST(DATEADD(dd, 8-(DATEPART(dw, GETDATE())), GETDATE()) AS DATE) [WeekEnd]
/*-- Week End
--7->Saturday --8-> Sunday...*/
Running this today will produce this result:
WeekStart WeekEnd
---------- ----------
2018-02-26 2018-03-04
if you have dates in your table, you can use them instead of the GETDATE()