Grouping a set of revolving years (Reporting Services 2016) - sql

Long time Stackoverflow user, but never actually asked questions. I'm currently stumped on a report request.
What I need to do with Report Builder is displaying data on pages grouped by three years, but here's the catch: I need it to be revolving.
I put together a query, but it's static:
SELECT distinct *, (dense_rank() over (order by [Year]) - 1) / 3 + 1 as [group]
FROM dwmy
It works fine for grouping years when I don't also need to still display past years. To give you an idea of what I'm trying to accomplish.
So, in the current year, it would look like:
In 2022:
In 2023:
As you can see, the current year still stays within the first group, but as a new year starts, it slowly works its way off the first page. The old groupings would still be on older pages, so users could still see the old groupings of 2019, 2020, 2021 or 2020, 2021, 2022.

Related

Add column of customer's past purchase total at time of current purchase and find rate of purchases that are from a returning customer - SQL

I am working with a table containing the purchase history for a shop. There is a purchase id, a date column and a customer id. I am trying (without much success so far) to do two things:
Add a column which for each purchase tells how many purchases the customer made before this (in the last month). I started by joining the table on itself but haven't got much further. I know I'll need to somehow filter the date so it only counts purchases before this date and not more than a month ago. Any suggestions on a simple way to tackle this?
The second thing I would like to see is what the weekly rate of returning customer transactions is. That is, what proportion of the purchases are by someone who purchased recently (in the last month). Ideally I would be able to graph this so from my sql queries I would like to end up with a date, weekly total (the 7 days up to the date) and weekly rate. I have been reading up on rolling windows and to be honest am having a bit of trouble getting my head around it. My SQL level is still quite low unfortunately. Any tips on a relatively simple way to do this would be much appreciated.
Thanks
I would need to see your data structure for the table(s) to better answer your question. But right off the top of my head is seems like you just need a simple SELECT COUNT.
So something like this would return all transactions from a single customer made in the past month:
SELECT COUNT(purchase_id)
FROM purchases
WHERE customer_id='some_customer_id'
AND date >= DATEADD(m, -1, GETDATE());
As for your second question you would probably want to setup a job (jenkins, ect..) that would run a query every month. The results of which you would plot. Checkout https://oss.oetiker.ch/rrdtool/ for graphing

Rolling months data by year_month in obiee

I just need to how to create Rolling months in Obiee. If I click for jan 2017,it should show datas from feb 2016.For past previous 12 Months it should show.
You will need a properly configured time dimension. As soon as you have that all the time series functionalities are at your disposal and will work immediately.
https://gerardnico.com/wiki/dat/obiee/obis/time_dimension
https://gerardnico.com/wiki/dat/obiee/obis/logical_sql/function_time

DAX - Running\Cumulative total and Leap Years

My problem is that I need to calculate a running total that works like a YTD.
The "year" in question is not a calendar year, it is an academic period.
TOTALYTD(..."28-02") nearly does the trick, however on leap years the 29th of Feb bleeds in to the next year's window.
I've tried CALCULATE(MyMeasure, DATESBETWEEN(...,...)) and this doesn't seem to work or I am just using the functions incorrectly.
To summerise, I need an expression that works exactly like TOTALYTD() but does not have the leap yer problem.
Thanks!
Unfortunately, I also experienced the same problem.
I created a dates table with a check to see if the year is a leap year.
Then I created this Dax Formula: Year End = IF(MAX( Dates[LeapYear] )=0,"02/28","02/29")
This worked perfectly, but if I add this to the TOTALYTD built-in formula like this Sales YTD = TOTALYTD( [Sales], Dates[Date], ALL( Dates[Date] ), [Year End]), it does not allow it - which is stupid - hopefully, they will allow this in the future.
I then found this answer, which answers your question but also doesn't because Power BI does not cater for it yet.
"You will need to write custom YTD measures instead" as per OwenAuger

Tables in a MVC application

I am learning MVC and SQL and I am trying to make an application that takes daily user data which is put in a day table. The day is linked to a week table and the weeks are linked to a year tables. I have entities for all 3. I am having problems with linking and orgainising them though. When I try link them it always uses their Primary Keys and so I am getting duplicate errors.
For example, when I have it that the user inputs the primary key value, and try to make Week 1 in, say 2015, but there is already a Week 1 in 2014, I get an error because they both have the same ID.
But when I have the computer automatically do the primary keys (so it goes 1, 2, 3, ...), I am only able get the days linked to the primary key. E.g. I have WeekID (the Primary Key) and WeekNo in the model. I have week 1 and 2 made and filled out, then go to make week 3 but accidentally make WeekNo 4, it's WeekID would still be 3 as it is the third week made. I delete it and make a new one and put in 3 as the WeekNo, then the WeekID is 4. This is a problem because then when I make a day, and click the dropdown menu for picking the week it goes in, I get the options "1, 2, 4" rather than "1, 2, 3" because it is using the primary keys. Even if the user never makes a mistake and has to delete it, this is still a problem when the next year comes around and the week numbers repeat.
My question is, does any one know how I could go about making it so that there are no duplicate problems and that they can be linked by something other than the primary key? Basically, when the user makes a week, they can select what year it goes into out of available years from the create page of Week. And the same when making a day, that they can select which week it goes in by number. Has any one encountered this kind of problem and gotten past it?
You are inventing calendar actually.
I would rather have one column with type of TIMESTAMP. And on the server side/ or via SQL queries calculate day/week/year number (you didnt mention which RBDM you use). That will prevent you from having relation issues and also will help you in manipulating TIMEZONES in the future.
Here is the post for JAVA sample where week number is calculated
Otherwise you will need to use composite primary keys for:
day - (DAY_ID, WEEK_ID, YEAR_ID)
week - (WEEK_ID, YEAR_ID)
year - (YEAR_ID)

automatically renewing records when the last day of year

I'm developing a c# application that consists of Document Incoming System for my police station.
In this system, variable document's contents are been saved to an SQL database. I must give them a "Document Number".
I'm achieving all of these, but i want that every years last day such as 31.12.2014, the numbers that have been given to a document like "2145" will turn to "1" at the the first day of year 01.01.2015.
So, the records must be 2014/2145. and the last days of years turns to 2015/1.
How can I achieve this?
You count the existing documents for the same year, then add one.
So if you want to store a document that belongs to 2013, you first count how many existing documents you have in 2013, then add one.
I can't write the sql for you, because you haven't described the data structure, but it should be simple enough using SQL COUNT, and DATEPART to retrieve only the year from a date field.