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)
 Average of Values in a month

Author  Topic 

Jumoke
Starting Member

2 Posts

Posted - 2007-01-22 : 05:40:16
hi guys,
i got range of values in a daily date format and the are of datatype smalldatetime but, the values are daily and i want to find average of those values based on month.
NOTE: the value may not necessary be up to 30 or 31 days
e.g average of values from january 1, 2000 to december 29, 2006 based on month i.e one value for each distinct month

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-22 : 05:44:09
select dateadd(month, datediff(month, 0, yourdatetimecolumn), 0), avg(anothercolumnnamehere)
from yourtablenamehere
where yourdatetimecolumn >= '20000101' and yourdatetimecolumn < '20070101'
group by dateadd(month, datediff(month, 0, yourdatetimecolumn), 0)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-22 : 06:00:41
select dateadd(month, datediff(month, 0, yourdatetimecolumn), 0), avg(anothercolumnnamehere),
min(anothercolumnnamehere),max(anothercolumnnamehere),count(anothercolumnnamehere),sum(anothercolumnnamehere)
from yourtablenamehere
where yourdatetimecolumn >= '20000101' and yourdatetimecolumn < '20070101'
group by dateadd(month, datediff(month, 0, yourdatetimecolumn), 0)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -