Previous business day/ year over year from null - sql-server-2012

I am trying to pull year over year data by date from a database, but when the previous year falls on a holiday, the row is null. For example, 5/25/15 is the same day previous year for 5/23/16 this year. However, since 5/25/15 was Memorial Day, it didn't even create a row. So, I need the solution to pull the data from the last available business day.
Any help out there?

Obviously you are doing something like this:
from thisyear
join prevyear on thisyear.mmdd = prevyear.mmdd
i.e. joining on the precisely same month and day, when you should be doing something like:
from thisyear
cross apply
(
select top(1) * prevyear
where thisyear.mmdd >= prevyear.mmdd
order by prevyear.mmdd desc
) prevyear
i.e. find the closest month and day that was before or even on the same day.

Related

In Bigquery SQL: How to fetch previous week, specified week and next week data?

Scenario: From bigquery, have to fetch the specified date's week data + its previous week data + its next future week data. Week starts is Wednesday.
Tried Query:
Select * from table
and extract(week(wednesday) from Calendar_Day) >= (extract(week(wednesday) from PARSE_DATE('%d/%m/%Y','21/10/2020')) - 1)
and extract(week(wednesday) from Calendar_Day) >= (extract(week(wednesday) from PARSE_DATE('%d/%m/%Y','21/10/2020') ))
and extract(week(wednesday) from Calendar_Day) <= (extract(week(wednesday) from PARSE_DATE('%d/%m/%Y','21/10/2020')) + 1)
But this is not working for me.
Need help in resolving this. Thanks in Advance!
EXTRACT the week as the code already does. and the year as the weeks repeat every year.
GROUP BY the week and year. At this point I find it handy to make a STRUCT from the remaining fields as it simplifies the remaining code.
make another query that uses the query which did the GROUP BY, I used a WITH. In this last query, LEAD and LAG the data with a WINDOW by week.
Here's an example from a public dataset.
WITH
data_by_week AS (
SELECT
EXTRACT(year FROM date) AS year,
EXTRACT(week(wednesday) FROM date) AS week,
struct(
SUM(new_tested) as total_new_tested,
sum(new_recovered) as total_new_recovered
) as week_data
FROM
`bigquery-public-data.covid19_open_data.covid19_open_data`
GROUP BY
year,
week )
SELECT
year,
week,
LAG(week_data) OVER window_by_week AS previous_week,
week_data AS current_week,
LEAD(week_data) OVER window_by_week AS following_week
FROM
data_by_week
WINDOW
window_by_week AS ( ORDER BY year, week)
ORDER BY
year,
week

BigQuery, Sum by week

I am using standard SQL and am trying to add the weekly sum for product usage by week.
Using code below, I was able to add to each row the respective week and year it falls into. How would I go about summing the totals for an item by week and outputting it in columns, say up to the last 8 weeks.
extract(week from Metrics_Date) as week, EXTRACT(YEAR FROM Metrics_Date) AS year
Image is my raw data with the week and year next to an item:
This image is of above raw data being analyzed further(grouping them together). Here is where I would want to add columns, current_week & firstday of week date, and a sum of that weeks totals.
Any help would be appreciated.
You don't need the extract() by the way, you can do truncation DATE_TRUNC(your_date, WEEK) and it will truncate it to the week, usually easier.
Also, because the result of the truncation is a date, you will have the first day of the week already.
The rest I believe you have it figured out already, but just in case:
SELECT DATE_TRUNC(your_date_field, WEEK) AS week, SUM(message_count) AS total_messages FROM your_table GROUP BY 1

Break down date by month and the week of each month using Sql

What I need is break down the date by month and the week of that month. For example, for '2020-01-06' I want to see January, 1 week, or for '2020-01-13', I want to see January, 2nd week.
Here is the code that wrote:
.....
distinct t1.incidentid
,t1.status as [CW_Staus],
t1.title
,t1.createddatetime
,datename(MM,t1.createddatetime) as Month
,datename(ww,t1.createddatetime) as Week
.....
Now, what I see is this
However, with the start of each month, I want to restart the week count. for '2020-02-01', I want to see Feb, 1week.
What am I missing in the code?
Thank you
You could try taking the day of the month and divide by 7:
SELECT DISTINCT
t1.incidentid,
t1.status AS [CW_Staus],
t1.title,
t1.createddatetime,
DATENAME(MM, t1.createddatetime) AS Month,
1 + ((DATEPART(day, t1.createddatetime)-1) / 7) AS WeekOfMonth
...
Keep in mind that the above logic assumes that you actually want to reset the count of weeks for each month, at the start of each month. More typically, there are ISO standard ways of counting the week in the year. There are edge cases to be considered, as any given year may not have an whole number of weeks.

sql to find the weekdays in the month from the current day

please help me with this. using SQL server 2008
I need to find the number of sales done on the current day.
then find the weekday from current date and based on that find the average of the sales on all those particular weekdays in the last month
ex:
select count(sales) from salestable where orderdate= getdate()
where it gives the count of the sales done on the current date
then I need to find out the average of the sales done on the same weekday for ex if today is Sunday find the average of the sales done in the last month on all Sundays in that month.
I recommend that you borrow the data warehousing technique of creating a Calendar table that you pre-populate with 1 row for every date within the range you might need. You can add to it basically any column that is useful - in this case DayOfWeek and MonthID. Then you can eliminate date math entirely and use joins - sort of like this (not complete but points you in the right direction):
select count(salestable.sales) as salescount, a.salesavg
from salestable
join calendar on salestable.orderdate = calendar.calendardate
join (
select monthid, dayofweek, avg(salestable.sales) as salesavg
from salestable
join calendar on salestable.orderdate = calendar.calendardate
group by monthid, dayofweek) as a
on calendar.monthid = a.monthid and calendar.dayofweek = a.dayofweek
where calendar.calendardate = getdate()
You create and populate the calendar table once and reuse it every time you need to do date operations. Once you get used to this technique, you will NEVER go back to date math.
For this kind of queries are Common Table Expressions very usefull. Then you can use DATEPART function to get day of week.
This solution is also untested and intended to just point you in the right direction.
This solution uses a co-related sub-query to get the average sales.
select
order_date,
count(sales) total_sales,
(select avg(sales)
from sales_table
where order_date between dateadd(day,-30,#your_date) and #your_date
and datepart(WEEKDAY,order_date) = datepart(WEEKDAY,#your_date)
) avg_sales_mth
from sales_table
where order_date = #your_date

How to calculate Last Week of Month by WeekNO and Year in SQL

I want to calculate Last Week Number of Month in SQL. I am having Week Number and Year.
Eg. If I pass WeekNo=51 , Year=2008 , than function should return LastWeekofMonth= 52.
I want to calculate Week number using below standards.
According to ISO 8601:1988 that is used in Sweden the first week of the year is the first week that has at least four days within the new year.
So if your week starts on a Monday the first Thursday any year is within the first week. You can DateAdd or DateDiff from that.
Please Help me..........
Thanks in advance.
SELECT WEEK(LAST_DAY(STR_TO_DATE('2008-51-Mon', '%x-%v-%a')));
Should do the trick for getting the last week number of month with MySQL :
I first convert to a date, then I get the last day of the month (here: 2008-12-31), then I compute the week of the last day of the month (52).
It should be easy to turn it into a function.
Hope this helps.
This is fairly straightforward if you use a calendar table. The month you need is given by this query.
select iso_year, month_of_year
from calendar c
where iso_year = 2008 and iso_week = 51
group by iso_year, month_of_year
--
iso_year month_of_year
2008 12
So you can use that result in a join on the calendar table, like this.
select max(c.iso_week) as last_week_of_month
from calendar c
inner join
(select iso_year, month_of_year
from calendar c
where iso_year = 2008 and iso_week = 51
group by iso_year, month_of_year) m
on m.iso_year = c.iso_year and m.month_of_year = c.month_of_year;
--
last_week_of_month
52
Here's one example of a calendar table, but it's pretty thin on CHECK constraints.
If you're using SQL Server, you can perform a calculation by using a master table, without creating a calendar table. This fellow gives you a very good explanation, which I recommend that you read. His SQL for calculating the first and last Sundays of each month can be adapted for your use:
declare #year int
set #year =2011
select min(dates) as first_sunday,max(dates) as last_sunday from
(
select dateadd(day,number-1,DATEADD(year,#year-1900,0))
as dates from master..spt_values
where type='p' and number between 1 and
DATEDIFF(day,DATEADD(year,#year-1900,0),DATEADD(year,#year-1900+1,0))
) as t
where DATENAME(weekday,dates)='sunday'
group by DATEADD(month,datediff(month,0,dates),0)
Edit: Once you have the date of the Thursday, you can get the week number from that date like this:
DECLARE #Dt datetime
SELECT #Dt='02-21-2008'
SELECT DATEPART( wk, #Dt)