Make a View for forecasting based on structure of a View with existing
data in SQL
In my database I have a View Revenues with fields ProductID, EditionID,
Year, January, February, March,..., December.
I need to display in Excel for each ProductID and EditionID all the year
and months revenues in a single record. In order to do that I execute the
query:
SELECT *
FROM Revenus r1
JOIN Revenues r2
ON r1.ProductID = r2.ProductID
AND r1.EditionID = r2.EditionID
WHERE r1.[Year] <> r2.[Year]
That works perfectly. However since the sheet will be used also for
forecasting purposes, I have been asked to have the Year and months with a
10 years horizon. That means besides the data already existing in the
database, given starting year 2013, I must have years and related month
till 2023. The months related data must be equal to zero if there is no
Year yet in the database.
I tried with pivot table but it does not sound as the right solution. I
might try with a foreach but I read on the web that is a bad practice. The
best solution, yet a bit dirty, might be to join the existing data with a
View built on the fly with empty records for months and increase the year
for each "iteration". Anybody might help? Thanks
No comments:
Post a Comment