SSRS SQL I need to display data for dates used in the parameter and the previous month - sql

I have an SSRS report with parameters for Created On Start and Created On End. Users run this manually and choose the date range to display records for. I need to display in two different columnns the records for the month the user entered in the parameters and the previous month for the dates used in the parameters.
For example the user uses the the following dates in the parameters:
Start Date: 03/01/2016 EndDate: 03/31/2016
The Report should display in one column the records for march 2016 and next to it the records for february 2016

You could write one query which queries both months.
Add a field that will act as the column label eg format the date as the first of the month.
Then create a pivot table to show the two months as the columns with the usual rows .
EDIT - new details
So:
dateStart = '2016-03-01'
dateEnd = '2016-03-31'
These could be less than the whole month, but should be in the same month. prevStart = DATEADD(month, DATEDIFF(month, '2000-01-01', dateStart)-1, '2000-01-01')
the first day of the previous month.
Use similar for the prevEnd to calculate the last day of previous month.
OK. Now build your select:
SELECT xxxx, yyyy, zzzz
, DATEADD(month, DATEDIFF(month, '2000-01-01', createdOnDate), '2000-01-01') as MonthCol
FROM tables
WHERE (createdOnDate>= prevStart and createdOnDate<=prevEnd)
OR (createdOnDate>= dateStart and createdOnDate<=dateEnd)
Build a pivot table style grid with monthCol as the heading of the columns and your usual data as the rows. That way you can get your "previous Month" columns as well as the date range that you selected

Related

Need help aggregating custom months into a snapshot field

I am working with a dataset that has a continues date field and I want to aggregate it at a monthly level where the month ends on the 15th day of the month. So each snapshot date would go from the 15th of the month to the 14th of the following month.
Example: Snapshot Date = 7/15/2021 would correspond with the date range of 6/15/2021 through 7/14/2021.
Is there an easy way to do this for all months in the table using SQL Server.
Just subtract 14 days and convert to a year/month format. One trick is to move everything to the last day of the month:
select eomonth(dateadd(day, -14, datecol)), count(*)
from t
group by eomonth(dateadd(day, -14, datecol));

Recurring Date Calculator SQL

I'm looking to create a recurring date calculator that will correspond to EVENTS on a specific calendar date. Each event has its own rule.
For example, for EVENT A rule is "Occurs Tuesday monthly with the start date Tuesday 9-17-2019. I'm using a time dimension table with every date populated until 2025. Using these dates I was using in the WHERE clause
WHERE dayname = 'tuesday' and ( DATEDIFF(DAY, '2019-09-17', Calendar_Date) % 28 ) = 0 to get monthly tuesdays.
But I'm running into issues when there are two 5 Tuesdays in a month. 3-3-2020 and 3-31-2020. Need date populated to be 4-7-2020 instead of 3-31-2020.
Can anyone help with a solution?
With your query, you're pretty close. All you need to do is to then sub-select
SELECT MIN(CalendarDate) AS CalendarDate
FROM (your query goes here) AS d
GROUP BY YEAR(CalendarDate), MONTH(CalendarDate);

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

Combining year column and month column to date

In my database, I have a report_year column (char(4)) and a report_month column (varchar(2)). I am making an ssrs report that would use a stored procedure and would pull data from this table and my parameters are the date and year. I am succesful at doing this by casting both of the columns and concatenating them, also adding a "/" in between. So in SSRS report, the parameter that users need to put is the month and date (ex. 09/2016).
Users want a drop down to get the dates. Since my parameter is a varchar, it would ask literally for the month and the year formatted above. Is there anyway to cast this to date without the day itself, only just the month and the year? I tried datediff and dateadd functions but I am not having any luck.
Select BegMonth = cast(Replace('09/2016','/','/01/') as date)
,EndMonth = EOMonth(cast(Replace('09/2016','/','/01/') as date))
Returns
BegMonth EndMonth
2016-09-01 2016-09-30

Access query to pull previous month's data

I have a table in Access 2013 (Table1) that contains the following columns:
ID (pk), ReportDate, Amount
The most current data is 30-50 days old. For example, today (6/22/16) the most recent data would be the 5/1/16 row, as the 6/1/16 data won't be entered until mid-July. (All dates in the ReportDate column are the 1st of the month, i.e.: 4/1/16, 5/1/16, etc.)
I need to write a query that will do a 6-month lookback, but exclude the most current month's data.
So, for example, if I ran the query today (6/22/16), I would only get the rows that correspond to the following months:
12/1/2015
1/1/2016
2/1/2016
3/1/2016
4/1/2016
The data for 5/1/16 should be excluded, as it's the most recent month.
I can pull the previous 6 months worth of data with setting the criteria (in QBE) for ReportDate to>=DateAdd("m",-6,Date()), but I can't seem to figure out how to exclude the most recent month.
This should give you the start date of the most recent month in your table:
SELECT Max(ReportDate) AS MaxOfReportDate
FROM Table1;
If that is the month you want to exclude, use that query as a subquery which you cross join back to the table. Then you can use a WHERE clause with a BETWEEN condition whose end points are determined by DateAdd() expressions based on MaxOfReportDate:
SELECT t.ID, t.ReportDate, t.Amount
FROM
Table1 AS t,
(
SELECT Max(ReportDate) AS MaxOfReportDate
FROM Table1
) AS sub
WHERE
t.ReportDate BETWEEN DateAdd('m', -6, sub.MaxOfReportDate)
AND DateAdd('m', -1, sub.MaxOfReportDate);