Solved

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

Posted on 2014-04-11
5
377 Views
Last Modified: 2015-09-22
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

Open in new window

fiscalperiod-table.txt
itemquantities-view.txt
0
Comment
Question by:metropia
  • 2
  • 2
5 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher 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
    @reporting_date < DATEADD(DAY, 1, fp.EndDate)


SELECT
      sicl.ItemNumber
,      SUM(CASE WHEN fp.StartDate >= DATEADD(YEAR, -1, DATEADD(DAY, -1, @reporting_fiscal_period_startdate))
             THEN sicl.InvoiceQuantity ELSE 0 END) AS [Last 12 Months]
,      SUM(CASE WHEN fp.StartDate >= DATEADD(YEAR, -2, DATEADD(DAY, -1, @reporting_fiscal_period_startdate))
              AND fp.StartDate < DATEADD(YEAR, -1, DATEADD(DAY, -1, @reporting_fiscal_period_startdate))
             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
      fp.StartDate >= DATEADD(YEAR, -2, DATEADD(DAY, -1, @reporting_fiscal_period_startdate))
GROUP BY
      sicl.ItemNumber
,      sicl.FiscalYear
,      sicl.FiscalPeriodCode
,      fp.StartDate
,      fp.EndDate
ORDER BY
      sicl.FiscalPeriodCode ASC
,      sicl.ItemNumber
0
 

Author Comment

by:metropia
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

by:ScottPletcher
ScottPletcher 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

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

thank you very much Scott :)
0
 
LVL 65

Expert Comment

by:Jim Horn
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now