Record Date within Report Query - mysql

I am dissecting a report that was written by a previous employee. Can someone help me understand these lines?
Essentially I have been asked to find out if the report is pulling 2017 or 2018 data and these two lines are the only parameters within them.
AND RecordDate >= CASE WHEN MONTH(GETDATE()) >=10 THEN DATEFROMPARTS(YEAR(GETDATE()), 10,1) ELSE DATEFROMPARTS(YEAR(GETDATE())-1,10,1) END
AND RecordDate < CASE WHEN MONTH(GETDATE()) >=10 THEN DATEFROMPARTS(YEAR(GETDATE())+1, 10,1) ELSE DATEFROMPARTS(YEAR(GETDATE()),10,1) END

This pair of conditions dynamically filter the dataset for the a dynamic period that ranges from the first of October to end of September, like a fiscal year.
CASE WHEN MONTH(GETDATE()) >=10
THEN DATEFROMPARTS(YEAR(GETDATE()), 10,1)
ELSE DATEFROMPARTS(YEAR(GETDATE())-1,10,1)
END
The first case expression computes the lower bound of the period: it gives you October 1st of last year if current date is before October 1st, else October 1st this year (i.e. a new fiscal year started).
The other expression uses similar logic to compute the upper bound.

Related

Getting the Week Number for the Month Crystal Reports Formula

I have a Crystal Report Grouped by (Day,Week,Month).
I want to be able to display the "Week Number" for the month. Ex: 1st Week Of July, 2nd week of July, 3rd Week of July, etc. on the "Week" Group Header.
I have tried using a Formula
Totext(DatePart("ww", {Command.TransactionDate}),0)
But the result is the "Week Number" for the year EX: 33,34,35. Any help would be very much appreciated
Use an expression like this:
datevar yourDate := currentdate;
Datepart("ww",yourDate)+1
- Datepart("ww",yourDate - Day(yourDate)+1)
of course, replace the variable assignment with your date.
The logic is to get the week number of the date (plus 1) and subtract the week number of the 1st of the current month.

SSRS 2008 R2 Report. Change month grouping from calender month

I have a report that outputs relatively simple counts of clients. It outputs everything from a system as data parameters are not required. They want to see everything.
The thing that is throwing me is, they want months grouped by the 24th of one month to the 23th of the next month.
For example, the month of October 2016 should count all data between the 24th October 2016 TO 23rd of November 2016, the previous Septeber should group everthing between 24/09/2016 to 23/10/2016 etc etc.
In short, is it possible to group month using 24th of that month to the 23rd of the next month?
Happy to provide more information, a bit new to advanced ssrs tasks like this. I did search for solutions like this but couldnt find anything suitable. Any help greatly appreciated.
This requirements can be implemented by enhancing your query. Say f.e., you have the following:
select
t.Date,
t.AggregatedField
from SomeSchema.SomeTable as t
You can do next:
select
MonthNumber =
case
when DATEPART(DAY, t.Date) >= 24 then DATEPART(MONTH, t.Date)
else DATEPART(MONTH, t.Date) - 1
end,
t.AggregatableField
from SomeSchema.SomeTable as t
The output you will get will be like (month_number, field_to_aggregate). You then need to group data by that month_number, you can do that directly in SQL, or using RDL report grouping (consider that first provides the best performance).
To construct month name, you will probably need to extract year number to like this:
DATEPART(YEAR, t.Date)
and then construct date using month number anhd year number.

Is it possible to loop in MySQL for sales data?

I am trying to build a useful query for our sales team to see how many sales our business has made in the current month, compared to the same point in the previous months.
So if today is the 14th of September, I want to compare how many sales we'd made between 1st - 14th of August and so on to see if we are up or down.
I have created the query to pull the data, but it's not in any kind of loop. Can anyone suggest a way to do this please? Below shows me the data for July 2015...
SELECT CONCAT(MONTH(OrderDate),'-',YEAR(OrderDate)) AS MontyYear,
COUNT(sw_orders.OrderNumber) OrderCount,
SUM(Gross) GrossIncome
FROM
orders
WHERE
orders.MasterOrderNumber = ''
AND Date(OrderDate) >= '2015-07-01'
AND Date(OrderDate) <= Concat('2015-07-', DAY(CURDATE()))
order by orders.ordernumber;
It feels like I need a variable that is the month number, so starts at "1" for January - then counts up per loop and use is used in the OrderDate part of the query?
I would not use string literals in the query, but make use of MySql's DAY and MONTH function:
WHERE orders.MasterOrderNumber = ''
AND DAY(OrderDate) <= DAY(CURDATE())
GROUP BY MONTH(OrderDate)

How to do YTD, MTD calculations based on the Today()

I am very new to SSRS, I have below scenario.
I have to calculate measures for YTD, MTD, Last 7 Days and Previous day based on the today(). I need to use these YTD, MTD, Last 7 days and Previous day variables in the calculations while writing the IIF syntax
For example:
sum(IIF(Year="this year is Year to Date", value, sales)
sum(IIF(CreatedDate="the last 30days values ",sales)
Can any one please tell me how to achieve this
Thanks
You can use the YEAR function to get the year of a date field. An IIF statement is used to evaluate an expression (in your case if the date is within the current year) then the desired result if the expression is true then the result if false. So your YTD formula could look like:
=SUM(IIF(YEAR(Fields!YourDateField.Value) = Year(TODAY()), Fields!Sales.Value, 0)
This reads: The sum of (if the year = current year then Sales else 0).
The last 30 days is similar but you would use the DATEADD function to figure what the date was 30 days ago:
=SUM(IIF(Fields!YourDateField.Value >= DATEADD("d", -30, TODAY()), Fields!Sales.Value, 0)
https://technet.microsoft.com/en-us/library/aa337194(v=sql.100).aspx

SQL fiscal quarter totals... I've already got the calendar ones

Here's the SQL query I have for the calendar year's quarterly totals
SELECT
SUM(CASE WHEN WEEK(LAST_DAY) <= 13 THEN BILLABLE END) AS Q1
,SUM(CASE WHEN WEEK(LAST_DAY) >= 14 AND WEEK(LAST_DAY) <= 26 THEN BILLABLE END) AS Q2
,SUM(CASE WHEN WEEK(LAST_DAY) >= 27 AND WEEK(LAST_DAY) <= 39 THEN BILLABLE END) AS Q3
,SUM(CASE WHEN WEEK(LAST_DAY) >= 40 AND WEEK(LAST_DAY) <= 53 THEN BILLABLE END) AS Q4,
Emp_Name
FROM 'emp_info'
WHERE YEAR(LAST_DAY) = YEAR(CURRENT_TIMESTAMP)
GROUP BY Emp_Name
I need to shift it so it gives me the fiscal year (July 1 to June 30) totals.
I know when doing so the week numbers will need to start on July 1, not January 1 and there'll probably be a case statement in there somewhere. But I can't get it to come out correctly.
If I were you, I'd store fiscal quarters in a table. Then your queries would be dead simple. And not only would they be dead simple, it would be obvious when they were correct.
Try this query -
SELECT
Emp_Name,
IF(DATE(LAST_DAY) <= DATE('2011-6-30'), YEAR(LAST_DAY) - 1, YEAR(LAST_DAY)) f_year,
QUARTER(LAST_DAY) quarter,
SUM(BILLABLE)
FROM
emp_info
GROUP BY
Emp_Name,
f_year,
quarter;
You are aware that your query doesn't divide year into 4 quarters by month? Instead of using the WEEK() function use MONTH().
By operating on months and years (YEAR()) calculating sum for fiscal year will be easy.
Your real problem is going to be that generally speaking, date and time functions will only work on ISO/(Other common) calendar date/time objects, but you need them to work on a company-specific fiscal calendar.
This is why Calendar files are used, as a translation between fiscal and ISO calendars.
Generate your calendar fileout for a few years in either direction, then you can join to it, restricting/grouping by fiscal year and period. No case statements needed.
I also recommend storing most dates in your tables as actual ISO dates, then only translating/using fiscal dates when actually necessary - we have some summation tables here which are keyed off fiscal year/period, but which display shopping-behaviour data to customers (who don't know/care when our fiscal year is)...