Solved

# get last 12 months quantites only for months that have their fiscal period completed

Posted on 2014-04-11
394 Views
I am writing a stored procedure that consists of Item Quantities for records that have completed their Fiscal Period in the last 12 months, and the last 24 months

The output should be like:

Item Number | Last 12 Months | Prior 12 Months

First I need to be able to figure out if the date that the report is run falls in a Fiscal Period that is Closed or Open, and only return the records for those that fall in the Closed Fiscal Period.

Let's say that i run the report today, and pass getdate() to the store procedure.

That is: 4/11/2014
That date is within the fiscal period 076, that uses Fiscal Start Date: '2014-03-30' , and Fiscal End Date: '2014-04-26'

The results should not include any records of April, because April's fiscal period has not yet closed.

The view that contains the quantities has the Fiscal Period Code in it.
There is a separate table that contains the Fiscal Start Date and Fiscal End Date (and Fiscal Period Code as well)

I think I need to do a left join to the Fiscal Period table and get the Start and End Dates and then check if today's date falls within those fiscal dates to include it or not.
Then I need to look in the last 12 months (last 12 months starting from the most recent close fiscal period) and the prior 12 months to those too.

I know it sounds like a puzzle, I am trying to explain what I learned so far and be clear, please understand that, and if I need to be more clear just tell me in which points.

I am including some sample data of the Item Quantities view for two items only. And data from the the Fiscal Period table.

I hope some one can offer me some help on this task.

Thank you very much.

my query so far:

``````	SELECT
sicl.ItemNumber
,	SUM(sicl.InvoiceQuantity)
,	sicl.FiscalYear
,	sicl.FiscalPeriodCode
,	fp.StartDate
,	fp.EndDate
FROM
cost.vStd_SalesInvoiceCostLine sicl LEFT OUTER JOIN
dim.FiscalPeriod fp ON
sicl.FiscalPeriodCode = fp.FiscalPeriodCode
WHERE
ItemNumber COLLATE DATABASE_DEFAULT IN ('11041', '12081')
GROUP BY
sicl.ItemNumber
,	sicl.FiscalYear
,	sicl.FiscalPeriodCode
,	fp.StartDate
,	fp.EndDate
ORDER BY
sicl.FiscalPeriodCode ASC
,	sicl.ItemNumber
``````
fiscalperiod-table.txt
itemquantities-view.txt
0
Question by:metropia
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 69

Accepted Solution

Scott Pletcher earned 500 total points
ID: 39994849
Maybe something like below.  I don't have data to test it, so haven't tested it yet (sorry, I really don't have time to convert text data to CREATE TABLEs and SQL INSERTs -- that would take me multiple times more time than the SQL query itself below!).

DECLARE @reporting_date datetime
SET @reporting_date = GETDATE()
----------------------------------------------------------------------------------------------------

DECLARE @reporting_fiscal_period_startdate datetime
DECLARE @reporting_fiscal_period_enddate datetime
SELECT
@reporting_fiscal_period_startdate = fp.StartDate
,   @reporting_fiscal_period_enddate = fp.EndDate
FROM
dim.FiscalPeriod fp
WHERE
@reporting_date >= fp.StartDate AND

SELECT
sicl.ItemNumber
THEN sicl.InvoiceQuantity ELSE 0 END) AS [Last 12 Months]
THEN sicl.InvoiceQuantity ELSE 0 END) AS [Prior 12 Months]
,      sicl.FiscalYear
,      sicl.FiscalPeriodCode
,      fp.StartDate
,      fp.EndDate
FROM
cost.vStd_SalesInvoiceCostLine sicl INNER JOIN
dim.FiscalPeriod fp ON
sicl.FiscalPeriodCode = fp.FiscalPeriodCode
WHERE
ItemNumber COLLATE DATABASE_DEFAULT IN ('11041', '12081') AND
fp.EndDate < @reporting_fiscal_period_startdate      AND
--get only last two closed fiscal periods
GROUP BY
sicl.ItemNumber
,      sicl.FiscalYear
,      sicl.FiscalPeriodCode
,      fp.StartDate
,      fp.EndDate
ORDER BY
sicl.FiscalPeriodCode ASC
,      sicl.ItemNumber
0

Author Comment

ID: 39995331
would you be willing to explain to me what is happening in the case statements and also at the end on the where clause?

thank you much
0

LVL 69

Assisted Solution

Scott Pletcher earned 500 total points
ID: 39995343
The WHERE clause restricts the rows selected to only those for the past two years.

The CASE statements restrict each total to only the specific 12 month's data they should include.  A simple SUM() won't work, since each total should only be a specific 12 months out of the 24 months total data being read.
0

Author Closing Comment

ID: 39999591
excellent solution! it fitted my need very well.

thank you very much Scott :)
0

LVL 65

Expert Comment

ID: 40989021
metropia - I just kicked out an article that deals specifically with Fiscal calendar planning --> SQL Server Calendar Table:  Fiscal Years.   Let me know if this helps you, and if yes please click on the 'Good Article' button and provide some feedback.  Thanks.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
###### Suggested Courses
Course of the Month7 days, 22 hours left to enroll