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
 General SQL Server Forums
 New to SQL Server Programming
 Sum Amount on Weekly Basis
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nirene
Yak Posting Veteran

India
92 Posts

Posted - 04/05/2013 :  02:24:33  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 04/05/2013 :  02:41:21  Show Profile  Reply with Quote
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

India
92 Posts

Posted - 04/05/2013 :  03:09:42  Show Profile  Reply with Quote
Thanks for the code and correction of result Visakh

Nirene
Go to Top of Page

Jeff Moden
Aged Yak Warrior

USA
649 Posts

Posted - 04/07/2013 :  12:04:17  Show Profile  Reply with Quote
@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

India
52309 Posts

Posted - 04/09/2013 :  00:12:37  Show Profile  Reply with Quote
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
  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.08 seconds. Powered By: Snitz Forums 2000