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 2005 Forums
 Transact-SQL (2005)
 SQL Grouping by Date

Author  Topic 

fezz
Starting Member

12 Posts

Posted - 2010-11-23 : 11:11:29
Hi All, Can someone point me in the correct direction for this issue:

I have data like below, i need to create a table which tells me per month how much money is due in, so for ID 1 the value is 3000 over 3 months so 1000 a month starting in January through to March


ID, Start_Date, Period_Months, Value
1, '01-01-2010 00:00:00.00', 3, 3000
2, '01-04-2010 00:00:00.00', 8, 80000
3, '01-08-2010 00:00:00.00', 1, 123456

The End Result needs to look something like this

Month, Total_Value
January-10,1000
February-10,1000
March-10,1000
April-10,10000
May-10,10000
June-10,10000
July-10,10000
August-10,124456
Sept-10,10000
Oct-10,10000
Nov-10,10000

Does anyone have any example code for this?? Thanks

Pete

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2010-11-23 : 16:12:51
If you had a calendar table...
[CODE]select
cast(Month(c.CalDate) as varchar)) + '-' + right(cast(Year(c.CalDate) as varchar)) as [Month],
Value / Period_Months as Total_Value
from
MyTable t
inner join
Calendar c
on
Day(c.CalDate) = 1
and c.CalDate between t.Start_Date and DateAdd(Month, Period_Months - 1, t.Start_Date)[/CODE]

=======================================
No matter what side of the argument you are on, you always find people on your side that you wish were on the other. -Jascha Heifetz, violinist (1901-1987)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-24 : 09:48:04
if no calendar table create one using below

http://visakhm.blogspot.com/2010/02/generating-calendar-table.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -