I have this problem that I have been trying to solve with vbscript without a great sucess.

What I have is two tables. the first is a table with production numbers, the 2nd is a table that contains the last time that an event occurred for a particular production line and for a specific task. each time an event is done, this table is updated. each task should be done after X amount of production is reached. what i need to do is give the user the point at which they are at at any given time

Example
Line Number Event Pos Install Date Production
1 Cleaning A 4/1/2004 1,200,000
1 Painting A 4/1/2004 1,200,000
1 Scraping B 4/2/2004 900,000
2 Cleaning A 4/2/2004 850,000
2 Painting A 4/2/2004 850,000
2 Scraping B 4/3/2004 750,000

What has to happen is that I must match up the line and event from the event table with the line in the production table and sum the production where RemoveDate (which becomes the new install date) is greater than the production date. then i need to take the amount of production done on the date that is equal to the removal date and add it to the total minus any production done prior to the RemoveShift (which becomes the new install date). our production begins with the 3rd shift followed by 1st then second.

So the calcuation would look something like this for Line 1 for cleaning
Total production for EntryDate >= 4/2/2003 = 12792750
Production for the EntryDate = 4/1/2003 = 280800
with 100000 on 3rd, 50000 on 1st, and 130800 on 2nd. there was 3300 ran before the cleaning started on 1st shift. so the net is 1st shift production 50000 - 3300 + 2nd shift production 130800 + the total ran from 4/2/2003 foward for a total of 12970250

Summary need to calculate on the fly where each line is on production for each event. must back out any production done prior to work. production for each day is order like this 3rd, 1st, 2nd. In my table RemoveDate and RemoveShift is the day that that event occured as I have another table that the net is entered into for historical data. each time an event occurs the EventDate table gets updated.

My Question is can this be done in SQL? If so how do I go about it?

Thanks,
Lee
here are DDL for the two tables, they only have 3 shifts of production data for lines 1 and 2 for date that the events occured.

create table tblProd(EntryDate datetime, EQuipNum int, Shift int, Production int, PM bit)

insert into tblProd values('4/1/2003', 1, 1, 50000, 1)
insert into tblProd values('4/1/2003', 1, 2, 130800, 1)
insert into tblProd values('4/1/2003', 1, 3, 100000, 1)
insert into tblProd values('4/2/2003', 1, 1, 350000, 0)
insert into tblProd values('4/2/2003', 1, 2, 300000, 0)
insert into tblProd values('4/2/2003', 1, 3, 50000, 0)
insert into tblProd values('4/3/2003', 1, 3, 220000, 0)
insert into tblProd values('4/4/2003', 1, 1, 560000, 0)
insert into tblProd values('4/5/2003', 1, 2, 545000, 0)
insert into tblProd values('4/6/2003', 1, 3, 625000, 0)
insert into tblProd values('4/7/2003', 1, 1, 705000, 1)
insert into tblProd values('4/8/2003', 1, 2, 785000, 0)
insert into tblProd values('4/9/2003', 1, 3, 865000, 0)
insert into tblProd values('4/10/2003', 1, 1, 945000, 0)
insert into tblProd values('4/11/2003', 1, 2, 1025000, 0)
insert into tblProd values('4/12/2003', 1, 3, 1105000, 0)
insert into tblProd values('4/13/2003', 1, 1, 1185000, 1)
insert into tblProd values('4/14/2003', 1, 2, 1265000, 0)
insert into tblProd values('4/15/2003', 1, 3, 1345000, 0)

insert into tblProd values('4/1/2003', 2, 1, 250000, 1)
insert into tblProd values('4/2/2003', 2, 1, 350000, 0)
insert into tblProd values('4/2/2003', 2, 2, 340000, 0)
insert into tblProd values('4/2/2003', 2, 1, 350000, 0)
insert into tblProd values('4/3/2003', 2, 2, 220000, 0)
insert into tblProd values('4/3/2003', 2, 3, 200000, 0)
insert into tblProd values('4/3/2003', 2, 3, 22000, 0)
insert into tblProd values('4/4/2003', 2, 1, 56000, 0)
insert into tblProd values('4/5/2003', 2, 2, 54500, 0)
insert into tblProd values('4/6/2003', 2, 3, 62500, 0)
insert into tblProd values('4/7/2003', 2, 1, 70500, 1)
insert into tblProd values('4/8/2003', 2, 2, 78500, 0)
insert into tblProd values('4/9/2003', 2, 3, 86500, 0)
insert into tblProd values('4/10/2003', 2, 1, 94500, 0)
insert into tblProd values('4/11/2003', 2, 2, 102500, 0)
insert into tblProd values('4/12/2003', 2, 3, 110500, 0)
insert into tblProd values('4/13/2003', 2, 1, 118500, 1)
insert into tblProd values('4/14/2003', 2, 2, 126500, 0)
insert into tblProd values('4/15/2003', 2, 3, 134500, 0)

insert into tblProd values('4/1/2003', 3, 1, 2500, 1)
insert into tblProd values('4/2/2003', 3, 2, 3500, 0)
insert into tblProd values('4/3/2003', 3, 3, 2200, 0)
insert into tblProd values('4/4/2003', 3, 1, 5600, 0)
insert into tblProd values('4/5/2003', 3, 2, 5450, 0)
insert into tblProd values('4/6/2003', 3, 3, 6250, 0)
insert into tblProd values('4/7/2003', 3, 1, 7050, 1)
insert into tblProd values('4/8/2003', 3, 2, 7850, 0)
insert into tblProd values('4/9/2003', 3, 3, 8650, 0)
insert into tblProd values('4/10/2003', 3, 1, 9450, 0)
insert into tblProd values('4/11/2003', 3, 2, 10250, 0)
insert into tblProd values('4/12/2003', 3, 3, 11050, 0)
insert into tblProd values('4/13/2003', 3, 1, 11850, 1)
insert into tblProd values('4/14/2003', 3, 2, 12650, 0)
insert into tblProd values('4/15/2003', 3, 3, 13450, 0)

create table tblEventDate(RemoveDate datetime, InstallDate datetime, EQuipNum int, RemoveShift int, InstallShift int, EndingProd int, EventType varchar, SetPos varchar)

insert into tblProd values('4/1/2003', '3/28/2003', 1, 1, 3300, Cleaning, A)
insert into tblProd values('4/1/2004', '3/28/2004', 1, 1, 3300, Painting, A)
insert into tblProd values('4/2/2004', '3/27/2004', 1, 2, 200, Scraping, B)
insert into tblProd values('4/2/2004', '3/27/2004', 2, 3, 300, Cleaning, A)
insert into tblProd values('4/2/2004', '3/27/2004', 2, 3, 300, Painting, A)
insert into tblProd values('4/3/2004', '3/28/2004', 2, 1, 200, Scraping, B)
insert into tblProd values('4/3/2004', '3/28/2004', 3, 2, 350, Cleaning, A)
insert into tblProd values('4/1/2004', '3/28/2004', 3, 2, 350, Painting, A)
insert into tblProd values('4/2/2004', '3/27/2004', 3, 3, 100, Scraping, B)