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

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

Related

How to group dates as custom week numbers in SQL?

I have a series of email engagement dates, to create dashboard on QLIK. It has SQL Editor
I want to group a series of dates as Week 1, Week 2, and so on. My table has date column.
I am thinking along the lines for insert a column named "Week Number", based on the oldest date in the table, add 7 days range as week 1 and next 7 days range as Week 2 and so on.
In Qlik you can use the weekstart(Date) function or the week(Date) for just a week number. Either inthe script or as a calculated dimension in the chart.
Extra credit for year(Date)&'-'&week(Date) for 2019-23 etc
You can use datepart(wk, date_column) for grouping by week. You may want to add datepart(yy, date_column) to group by year and week.
You need to know the first day in your table was which day of the week, and then use the following script in SQL Server
declare #FirstDayOfTableWeekDay int = 2
SELECT CEILING( (CAST(ROW_NUMBER() OVER(ORDER BY [Date] ASC) AS float)+ CAST(#FirstDayOfTableWeekDay AS float)-1) / 7) AS WeekNumber
FROM YourTable

SQL Data From Prior Month End Row During Current Month

I am looking to get the # of employees listed during the current month shown with the number of employees from the last day of the prior month from a SQL Server Query.
So for every day in September, I want to show the number of active employees from the August 31st row under PMActive. Here is the select part of the SQL Query. Active total is in Table 1. Dates are in Table 2.
CAST(T2.Calendar_Date AS date) AS CalDate,
CAST(DATEADD(D,-(DAY(AD.Calendar_Date)),AD.Calendar_Date) AS date) AS PMEndDate,
T1.Active AS Active,
FROM F_Table1 T1
LEFT JOIN Table2 T2 ON Date=Date
I can't seem to code to get this data field without getting a NULL or the current day active employees. This is how the output should look.
Data output

DAX Counting Values in previous period(s)

I have a Month Column with the Month Field populated for each line for the 100K of lines of data I have.
I need to count the amount of times the Month Field is populated in the Previous Month (Period).
I also need to count the total amount of times the Month Field is populated in the Previous 11 months as well.
This is a rolling count for each months reporting that I do..
table name: 'ws pds' and field name [Month Tagged]
You can utilize the powerful time intelligence functions in DAX such as PARRALLELPERIOD to look at values from previous months. But in order to make use of these functions you need to create a calendar/date entity. Mark that entity as a Date table. And join to it by date from your "ws pds" table. The Date dimension should span the timeframe of your date with a continuous list of dates, one row per day.
Then your measure could look like this:
PreviousMonthCount=
CALCULATE (
COUNTROWS ( 'ws pds' ),
'ws pds'[Month Tagged] <> BLANK (),
PARALLELPERIOD ( Calendar[Date], -1, MONTH )
)

Select next value where it equals another value

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

Data for specific date

My report gets data for the 1st of the current month. Let's say the 1st has still not come then how would I make the report show the data for the 1st of the previous month.
Thanks.
Simply use a select top 1 from your table, filtering by extract(day from yourDateColumn) = 1 to get only the rows with the data for the 1st day of any month, and order them in descending order by your date column (order by yourDateColumn desc), so that you always get the 1st day of the last available month in your table.
Docs for Oracle EXTRACT function