Author |
Topic |
cidr
Posting Yak Master
207 Posts |
Posted - 2011-10-27 : 09:54:02
|
Hi there,I want to sum amounts by months. However, I want to sum values by the 2nd of each month to the 1st of next month for all months converned.Does anyone know what the easiest way to do this is?Thanks for your help |
|
X002548
Not Just a Number
15586 Posts |
|
cidr
Posting Yak Master
207 Posts |
Posted - 2011-10-27 : 10:03:52
|
create table dates( datecol datetime, Sales decimal(10,4) )insert into dates values('2010-01-01',14.00),('2010-01-05',14.00),('2010-01-01',14.00),('2010-01-10',14.00),('2010-01-11',14.00),('2010-02-17',11.00),('2010-02-01',14.00),('2010-02-02',15.00),('2010-02-20',14.00),('2010-02-27',17.00),('2010-02-08',14.00),('2010-02-28',14.00)select sum(Sales)from datesgroup by MONTH(datecol)I should really know how to do this but my mind is not working for me on this one |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-10-27 : 10:05:04
|
I'm sure there must be more to this but...CREATE TABLE myTable99(Col1 char(10), Col2 datetime, Col3 money)GOINSERT INTO myTable99(Col1, Col2, Col3)SELECT 'A', '09/11/2011', 50.00 UNION ALLSELECT 'A', '09/12/2011', 50.00 UNION ALLSELECT 'A', '09/13/2011', 50.00 UNION ALLSELECT 'B', '10/11/2011', 50.00 UNION ALLSELECT 'C', '11/11/2011', 50.00GOSELECT MONTH(Col2), SUM(Col3)FROM myTable99GROUP BY MONTH(Col2)GO DROP TABLE myTable99GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
|
|
cidr
Posting Yak Master
207 Posts |
Posted - 2011-10-27 : 10:33:19
|
Thanks Brett. However, what I'm asking for is months grouped together only by the 2nd day of the month to the 1st day of the next month for example:2/10/2011 to 2/11/2011So, everything from from the 2nd day up to and including the 1st of the next month... for each monthIs this something you can help with?Cheers |
|
|
X002548
Not Just a Number
15586 Posts |
|
cidr
Posting Yak Master
207 Posts |
Posted - 2011-10-27 : 11:18:28
|
em... I don't understand the question Brett. the name of the grouping is irrelevant, it's the technique to fetch the sum amount for the given groups of dates.Let's call it closing balanceCheers |
|
|
X002548
Not Just a Number
15586 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-27 : 12:44:16
|
[code]SELECT MONTH(DATEADD(dd,-1,Date)) AS MonthName,SUM(Col3)FROM tableGROUP BY MONTH(DATEADD(dd,-1,Date))[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
cidr
Posting Yak Master
207 Posts |
Posted - 2012-09-29 : 16:28:37
|
Came across this post again and never did say thanks for the help Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-01 : 10:36:53
|
no problemyou're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|