All Forums Database
sqlcoder9 1 post Joined 08/16
03 Aug 2016
sum for trailing 365 days for users

Hi 
I'm trying to find the sum of amount for an 'ID' trailing 365 days from the 'DT'. Below is a sample from the data I'm working on. The below table consists of dates when purchase was made , id and the amount. I wanted to calculate amount spent by an id trailing 365 days from the 'DT'. Any help on this will be appreciated. Thanks.

DT	ID	AMT
1/17/15	1	31.74
1/23/15	1	14.33
1/25/15	1	21.76
3/5/15	1	150
6/12/15	1	60
4/23/16	1	16.86
4/25/16	1	60
7/1/16	1	15.57
2/26/15	2	61.9
3/1/15	2	15
2/28/16	2	25

I want my output to be 

DT	ID	AMT	
1/17/15	1	31.74	31.74
1/23/15	1	14.33	46.07
1/25/15	1	21.76	67.83
3/5/15	1	150	217.83
6/12/15	1	60	277.83
4/23/16	1	16.86	76.86
4/25/16	1	60	136.86
7/1/16	1	15.57	92.43
2/26/15	2	61.9	61.9
3/1/15	2	15	76.9
2/28/16	2	25	40

 

You must sign in to leave a comment.