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
 General SQL Server Forums
 New to SQL Server Programming
 Sum Amount on Weekly Basis

Author  Topic 

nirene
Yak Posting Veteran

98 Posts

Posted - 2013-04-05 : 02:24:33
Hai,

My data

Code,Amount,Date
1,2000,01/12/2012
1,1500,05/12/2012
1,3500,08/12/2012
1,2340,10/12/2012
2,3450,02/12/2012
2,1550,05/12/2012

I want to sum the amount on weekly basis ie amount from monday to sunday to be summed up on code basis

Result Expected
Code,Sunday_Date,Summed_Amount
1,02/12/2012,2000
1,09/12/2012,5000
1,16/12/2012,2340
2,09/12/2012,5000

Thanks in advance

Nirene


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-05 : 02:41:21
i'm guessing your output has a slight typo for code 2 as value with 02/12 cant be merged with 05/12 as they occur in different weeks as per your definition


declare @t table
(
Code int,
Amount int,
[Date] datetime
)
insert @t
values(1,2000,'20121201'),
(1,1500,'20121205'),
(1,3500,'20121208'),
(1,2340,'20121210'),
(2,3450,'20121202'),
(2,1550,'20121205')

SELECT Code,DATEADD(dd,DATEDIFF(dd,0,[Date])/7 * 7 +6,0),SUM(Amount)
FROM @t
GROUP BY Code,DATEADD(dd,DATEDIFF(dd,0,[Date])/7 * 7 +6,0)
ORDER BY Code,DATEADD(dd,DATEDIFF(dd,0,[Date])/7 * 7 +6,0)


output
-----------------------------------------
Code WeekDate Total
-----------------------------------------
1 2012-12-02 00:00:00.000 2000
1 2012-12-09 00:00:00.000 5000
1 2012-12-16 00:00:00.000 2340
2 2012-12-02 00:00:00.000 3450
2 2012-12-09 00:00:00.000 1550



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

nirene
Yak Posting Veteran

98 Posts

Posted - 2013-04-05 : 03:09:42
Thanks for the code and correction of result Visakh

Nirene
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2013-04-07 : 12:04:17
@visakh16,

In your solution, the value of 2000, which occurred on 20121201 is listed for the WeekDate of 20121202. What is your WeekDate? The start of the week or the end of the week? If it's the start of the week, then the output is incorrect.

--Jeff Moden
RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".

First step towards the paradigm shift of writing Set Based code:
"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

When writing schedules, keep the following in mind:
"If you want it real bad, that's the way you'll likely get it."
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-09 : 00:12:37
quote:
Originally posted by Jeff Moden

@visakh16,

In your solution, the value of 2000, which occurred on 20121201 is listed for the WeekDate of 20121202. What is your WeekDate? The start of the week or the end of the week? If it's the start of the week, then the output is incorrect.

--Jeff Moden
RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".

First step towards the paradigm shift of writing Set Based code:
"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

When writing schedules, keep the following in mind:
"If you want it real bad, that's the way you'll likely get it."


see the logic i applied for week date portion. its end date for the week under consideration

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -