Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Converting Weeks to Months

Author  Topic 

scottsanpedro
Starting Member

3 Posts

Posted - 2012-12-07 : 05:51:58
Hi all. I have a difficult problem whereby the data I have is on weeks and I need to produce monthly reports.
I first thought is to calculate the number of days the week has in a particular month. So 7 days we don't have a problem, all totals are added in full. But at the end of the month the week may only have 3 days in the month.
So maybe I need to then have 3/7 of the week total in the previous month and 4/7 in the next.
If so then I will try and work out.
I just wondered if there was any other clever way.
Thanks in advance Scott

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-12-07 : 06:18:39
Create a calendar table with days in it.
Have a column for the week and another for the month.
Then you can agggregate using the month and week and also count the number of days in each week for the month.

It would be better if you converted the underlying data to be by day then you can report by week and month easily using that table.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

scottsanpedro
Starting Member

3 Posts

Posted - 2012-12-07 : 07:14:30
Thanks Nigel. I have created the calendar table and now having a play. Will let you know.
Thanks for your comments. Scott
Go to Top of Page

scottsanpedro
Starting Member

3 Posts

Posted - 2012-12-07 : 07:37:12
OK. Seems to have worked. Thanks again. Scott

WITH RES
AS
(
SELECT DISTINCT
FD_Year
,FD_Week
,CAST(CAST((SELECT COUNT(CalendarWeek) FROM Calendar WHERE CalendarYear = A.FD_Year AND CalendarWeek = A.FD_Week AND CalendarMonth = 1) as Float) / 7 as Float) as WeekPercent
,[FD_TotalInvoiceSale]
FROM [Pies].[dbo].[ForecastData] A
)
SELECT
FD_Year
,FD_Week
,WeekPercent
,FD_TotalInvoiceSale
,CAST(CAST(FD_TotalInvoiceSale as Float) * CAST(WeekPercent as Float) as Float)
FROM
RES
Go to Top of Page
   

- Advertisement -