| Author |
Topic |
|
odieb
Starting Member
2 Posts |
Posted - 2004-06-24 : 04:14:15
|
| Hi, I am struggling with the following query. I need to simplify it, but can't get my head around it - declare @End as datetime, @Start as datetime, @Over as int, @Minus as float, @Sum as floatselect @End = max(enddate) from testeps where enddate <= '19990331'-- to check what is actually usedselect * from testeps where enddate <= @Endand enddate > dateadd("yy", -1, @End)--!!select @Over = (sum(nomonths) - 12), @Start = min(startdate), @Sum = sum(earning)from testeps where enddate <= @Endand enddate > dateadd("yy", -1, @End)if @Over > 0 select @Minus = (earning * @Over / NoMonths) from testeps where Startdate = @Startelse set @Minus = 0select sum(earning) - @Minus '12month', @Sum 'sum'from testeps where enddate <= @Endand enddate > dateadd("yy", -1, @End)Details: I have records with a startdate, enddate and earnings. I need to get a sum of the earnings over a 12 month period, working back from the last end date found, closest to the date the user specified. Here is a sample of the data:startdate,enddate,nomonths,earning01 Jul 2003,31 Dec 2003,6,401.1999999999990201 Jan 2003,30 Jun 2003,6,481.8000000000000101 Jul 2002,31 Dec 2002,6,629.4500000000000501 Jan 2002,30 Jun 2002,6,659.5499999999990501 Jul 2001,31 Dec 2001,6,565.8400000000000301 Jan 2001,30 Jun 2001,6,459.9401 Jul 2000,31 Dec 2000,6,437.8199999999990301 Jan 2000,30 Jun 2000,6,432.8299999999990201 Jul 1999,31 Dec 1999,6,311.9499999999990201 Jan 1999,30 Jun 1999,6,203.501 Oct 1998,31 Dec 1998,3,150.501 Apr 1998,30 Sep 1998,6,247.001 Oct 1997,31 Mar 1998,6,268.5299999999990101 Apr 1997,30 Sep 1997,6,262.001 Oct 1996,31 Mar 1997,6,274.5799999999990201 Apr 1996,30 Sep 1996,6,261.2501 Oct 1995,31 Mar 1996,6,220.6800000000000101 Apr 1995,30 Sep 1995,6,215.001 Oct 1994,31 Mar 1995,6,193.84999999999901 Apr 1994,30 Sep 1994,6,167.501 Oct 1993,31 Mar 1994,6,183.2501 Apr 1993,30 Sep 1993,6,137.2501 Oct 1992,31 Mar 1993,6,146.0800000000000101 Apr 1992,30 Sep 1992,6,119.001 Oct 1991,31 Mar 1992,6,144.501 Apr 1991,30 Sep 1991,6,136.501 Oct 1990,31 Mar 1991,6,155.501 Apr 1990,30 Sep 1990,6,124.001 Oct 1989,31 Mar 1990,6,195.501 Apr 1989,30 Sep 1989,6,142.501 Oct 1988,31 Mar 1989,6,176.2501 Apr 1988,30 Sep 1988,6,110.7501 Oct 1987,31 Mar 1988,6,127.7501 Apr 1987,30 Sep 1987,6,69.7501 Oct 1986,31 Mar 1987,6,100.7501 Apr 1986,30 Sep 1986,6,63.501 Oct 1985,31 Mar 1986,6,81.001 Apr 1985,30 Sep 1985,6,49.7501 Oct 1984,31 Mar 1985,6,58.32999999999989901 Apr 1984,30 Sep 1984,6,38.1801 Oct 1983,31 Mar 1984,6,51.63000000000000301 Apr 1983,30 Sep 1983,6,36.38000000000000301 Oct 1982,31 Mar 1983,6,36.89999999999989901 Apr 1982,30 Sep 1982,6,34.07999999999989901 Oct 1981,31 Mar 1982,6,42.22999999999989701 Jan 1900,30 Sep 1981,6,42.75 |
|
|
odieb
Starting Member
2 Posts |
Posted - 2004-06-24 : 05:21:57
|
| figured it out ... here it is, if anybody's interested.declare @End as datetimeselect @End = max(enddate) from testeps where enddate <= '20000831'--'19990331' --'19980630'--select sum(earning) 'sum', sum(case when startdate < dateadd("yy", -1, @End) then (earning * datediff("m", dateadd("d",-1,startdate), dateadd("yy", -1, @End))/nomonths) else earning end) '12m'from testeps where enddate <= @Endand enddate > dateadd("yy", -1, @End) |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-06-24 : 07:38:42
|
| amazing! sometimes....just asking others to help inspires the author!another sqlteam.com success.......for no effort |
 |
|
|
|
|
|