Solved

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

Posted on 2014-04-11
5
374 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

758 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

20 Experts available now in Live!

Get 1:1 Help Now