SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Converting Weeks to Months
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

scottsanpedro
Starting Member

United Kingdom
3 Posts

Posted - 12/07/2012 :  05:51:58  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 12/07/2012 :  06:18:39  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
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

United Kingdom
3 Posts

Posted - 12/07/2012 :  07:14:30  Show Profile  Reply with Quote
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

United Kingdom
3 Posts

Posted - 12/07/2012 :  07:37:12  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000