Select next value where it equals another value - sql

I have a table of the fiscal year for 100 years. i.e.
I am wanting to do add a column which shows the fiscal year that each week_ending_date belongs to. So in the table above week number 1, and week_ending_date 2013-10-05 would belong to fiscal year ending 2014.
In short I simply want each value in the added column to be the year part of week_ending_date where the next week_number is 52.
Here would be the pseudo code of what I am trying to achieve.
SELECT
Week_Ending_Date,
(SELECT NEXT VALUE FOR Week_Ending_Date (***but as only year***) FROM Fiscal_Calendar WHERE FC.Week_Number = 52)
FROM Fiscal_Calendar AS FC
JOIN Shipped_Container AS SC
ON SC.Fiscal_Week_Ending = FC.Week_Ending_Date
Bare in mind this has 100 years in the table and so I can't select the last value and makes using WHERE difficult (for me).

One way you can do this is by subtracting 7 * week number, taking the year and adding 1:
select 1 + year(dateadd(day, -7 * week_number, week_ending_date))

Related

SQL week number for the whole table

How to create a new column which calculates week number but for the whole table ignoring year?
Desired output is as follows:
Appreciate any help :)
You can do this by calculating 1st day of week of oldest row, and then calculate day diff of 1st day of week of current row and coldest row, after that, divide it by 7 days plus 1 will give you the desired week number across the full table.
Assuming you are using MySQL and the first day of the week is Sunday:
WITH min_week_start AS (
SELECT
SUBDATE(MIN(record_date), dayofweek(MIN(record_date)) - 1) as week_start_date
FROM
record_table
),
record_week_start AS (
SELECT
record_date,
SUBDATE(record_date, dayofweek(record_date) - 1) as week_start_date
FROM
record_table
)
SELECT
record_week_start.record_date,
DATEDIFF(record_week_start.week_start_date, min_week_start.week_start_date) / 7 + 1 as week_num
FROM
record_week_start
CROSS JOIN
min_week_start

Generate custom start and end of months in SQL Server

I'm facing an issue while working with custom dates in T-SQL, we have a client that works with a different methodology of start and end of his month, instead of the default day 01 to start the month and ending in 31, 30 or 29, it's month start at day 26 and ends at 25 of the next month.
E.g., how usually is:
select sum(sales) as sales
from sales
where salesDate between '2020-09-01' and '2020-09-30'
-- or left(salesDate,7) = '2020-09'
Client custom month:
select sum(sales) as sales
from sales
where salesDate between '2020-08-26' and '2020-09-25' -- for setember
So, for this need, I have to calculate how many sales this client did from january until now, month per month, with this custom way... how can I do that?
Example of the query result I want to perform with this month determination:
This is a pretty awful situation. One method is to construct the first date of the month based on the rules:
select mon, sum(sales) as sales
from sales s cross apply
(values (case when day(salesdate) >= 26
then dateadd(month, 1, datefromparts(year(salesdate), month(salesdate), 1))
else datefromparts(year(salesdate), month(salesdate), 1)
) v(mon)
where v.mon >= '2020-01-01'
group by v.mon;
I would recommend adding the fiscal month column as a persisted computed column in the salesDate table so you can add an index and not have to worry about the computation.
Or, better yet, add a calendar table where you can look up the fiscal month for any (reasonable) date.

How to get last one month's data from a table based on current month and year?

I am facing some problem with the hive code.
My FROM TABLE is partitioned based on month, year and day. I came up with the following code to get the data I need. The logic is something like if the current mth is 01 then change the month to 12 and the year to yr - 1
else change month to mth - 1 and keep the year as is.
set hivevar:yr=2019;
set hivevar:mth=03;
set hivevar:dy=29;
SELECT * from
FROM table
WHERE
month = case when cast('${mth}' as int) = 01 then 12 else cast((cast('${mth}' as int) - 1) as string) end
AND year = case when cast('${mth}' as int) = 01 then cast((cast('${yr}' as int) - 1) as string) else '${yr}' end;
It is not working, my select * is coming empty. Please help.
desc table
From what i understand, you are trying to get data from the previous month given a date. If so, you can use inbuilt date functions to do it.
select *
from table
where concat_ws('-',year,month,day) >= add_months(date_add(concat_ws('-','${yr}','${mth}','${dy}'),1-'${dy}'), -1)
and concat_ws('-',year,month,day) < date_add(concat_ws('-','${yr}','${mth}','${dy}'),1-'${dy}')
The solution assumes year, month and day are of the format yyyy, MM and dd. If not, adjust them as needed
Also, you should consider storing date as a column even though you have it partitioned by year,month and day.

Selecting sets of data and creating a new column in SQL Server

In SQL Server can you select the first set of values (i.e. week numbers 1 - 52) give them another value in a new column, then select the next lot of values.
The problem I am having is the data table I am working on has week numbers for each financial year, which starts the first Sunday after 1 October. So it simply iterates 1 - 52 for each financial year.
I am trying to make a column in a view that grabs the first 52 gives them the a financial year value of 1, then grabs the next 52 and gives them a financial year value of 2 etc (obviously with year 1 starting at the first record). I do have the Week Ending Date column to work with also.
Here is a snippet of the table:
Is this possible?
Leave the Sundays and Octobers. If I understand correctly, you only need to assign a rank to each occurrence of week number in order of the ending dates.
Please try this (but use copy of the table or transaction to check first; of course T is name of your table):
update T
set fiscal_year = YearNumbers.FiscalYear
from T
inner join
(
select WeekEndingDate, WeekNumber, DENSE_RANK() over (partition by WeekNumber order by WeekEndingDate) as FiscalYear
from T
) as YearNumbers
on T.WeekEndingDate = YearNumbers.WeekEndingDate and T.WeekNumber = YearNumbers.WeekNumber

Get month name with different month start and end dates

Getting the month for the current date is, obviously, straight forward, but I'm needing to get the month name with a different end date.
I need to get the month name with the start date of the month being the first Thursday after the first Wednesday of the month and the end date of the month being the first Wednesday of the following month. It's for an accounting thing, so I'm not going to argue with the spec!
e.g. for 2014, January would run from 9th Jan - 5th Feb, February would run from 6th February - 5th March, March would run from 6th March - 2nd April.
I would suggest that you create a table with your 'accounting months' in it, having a start date, end date and month name columns.
You could then query this to find the row where your date is between the start and end dates and return the month name. Putting this into a scalar function would then allow it to be reusable and relatively easily updated for next years months as well.
I think, as per Paddy's answer, a lookup table is the simplest thing to do. Here's one way to generate the rows for it:
; With Numbers(n) as (
select 4 union all select 5
), Months as (
select CONVERT(date,'20010104') as StartDt,CONVERT(date,'20010207') as EndDt,
DATENAME(month,'20010103') as Month
union all
select DATEADD(week,n1.n,StartDt),DATEADD(week,n2.n,EndDt),
DATENAME(month,DATEADD(week,n1.n,StartDt))
from Months,Numbers n1,Numbers n2 --Old-skool join, just for once
where DATEPART(day,DATEADD(week,n1.n,StartDt)) between 2 and 8 and
DATEPART(day,DATEADD(week,n2.n,EndDt)) between 1 and 7 and
StartDt < '21000101'
)
select * from Months option (maxrecursion 0)
(CW since this is effectively just an extension to Paddy's answer but I don't want to edit their answer, nor is it suitable for a comment