Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get sum of amount for previous 3 months (dynamically)

Status
Not open for further replies.

compbiz

Programmer
Apr 3, 2003
2
0
0
IN
I have 3 tables
district
dist_code dist_name
11 LA
12 WA

demands
dist_name year demandfortheyear
LA 1999 3434.44

dues
dist_name month year currentdue
LA 1 1999 890
LA 2 1999 656
LA 3 1999 455
LA 4 1999 4554

if current year is 1999 and month is 4 I want to retrieve it from system date and get sum of currentdue for previous 3 months(1 to 3).what is the query.Do I need to use stored procedure for the crystal reports.
 
I haven't tested this, see if this is worth a shot:

select a.dist_name, a.month, a.year, b.accum
from dues a,
table (select sum(currentdue) from dues x
where date(digits(decimal(x.year,4,0)) || '-' ||
digits(decimal(x.month,2,0)) || '-01') >=
date(digits(decimal(a.year,4,0)) || '-' ||
digits(decimal(a.month,2,0)) || '-01') - 3 months
and date(digits(decimal(x.year,4,0)) || '-' ||
digits(decimal(x.month,2,0)) || '-01') <
date(digits(decimal(a.year,4,0)) || '-' ||
digits(decimal(a.month,2,0)) || '-01')
) as b (accum)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top