C
charlie
Hi,
My recent posts have all been related to one single (and I though
simple) problem. Basically I need to calculate the delta for a given
field using the current record and the previous record. The table must
be time ordered for the deltas to be calculated correctly. Right now
I'm trying to do this from ADO using SQL (e.g. open connection, do SQL
via conn.execute "SQL goes here").
I've tried a couple of different approaches. One came from SQL
Fundamentals book by John J Patrick:
**************************************************************
-- 16-11 Access SQL: Step 1
select price,
description
into sec1611
from l_foods
where price > 1.75
order by price,
description;
-- 16-11 Access SQL: Step 2
alter table sec1611
add column line_number counter;
**************************************************************
This seems to work most of the time, but not always. I believe that it
has to do with the fact that the alter column... is messing with the
order by from the select statement. He says in his book that you can
rely on the order of tables after select...order by with Access but
I've proved that this is not true.
My next attempt involved a self join....something similar to the
following:
**************************************************************
SELECT YourTable.YourDateField, (Select First(YourDateField) _
as NextDate from YourTable as [Temp] WHERE _
[Temp].[YourDateField] > [YourTable].[YourDateField]) _
AS NextDate FROM YourTable _
ORDER BY YourTable.YourDateField
**************************************************************
Once you have the new column with NextDate, its easy to subtract
YourDateField from NextDate. This too seems to work most of the time
too, however sometimes NextDate is not updated correctly.
I'm currently at a loss as to how to perform a delta calculation on a
field in a table that is robust and will work 100% of the time.
I'm not looking for someone to write a solution for me....but more
some help and pointers as to what could be going wrong and possible
places to look for solutions. I've combed the internet using Google
for hours and found nothing that seems any better than the examples
above.
Any help would be greatly appreciated
charlie
My recent posts have all been related to one single (and I though
simple) problem. Basically I need to calculate the delta for a given
field using the current record and the previous record. The table must
be time ordered for the deltas to be calculated correctly. Right now
I'm trying to do this from ADO using SQL (e.g. open connection, do SQL
via conn.execute "SQL goes here").
I've tried a couple of different approaches. One came from SQL
Fundamentals book by John J Patrick:
**************************************************************
-- 16-11 Access SQL: Step 1
select price,
description
into sec1611
from l_foods
where price > 1.75
order by price,
description;
-- 16-11 Access SQL: Step 2
alter table sec1611
add column line_number counter;
**************************************************************
This seems to work most of the time, but not always. I believe that it
has to do with the fact that the alter column... is messing with the
order by from the select statement. He says in his book that you can
rely on the order of tables after select...order by with Access but
I've proved that this is not true.
My next attempt involved a self join....something similar to the
following:
**************************************************************
SELECT YourTable.YourDateField, (Select First(YourDateField) _
as NextDate from YourTable as [Temp] WHERE _
[Temp].[YourDateField] > [YourTable].[YourDateField]) _
AS NextDate FROM YourTable _
ORDER BY YourTable.YourDateField
**************************************************************
Once you have the new column with NextDate, its easy to subtract
YourDateField from NextDate. This too seems to work most of the time
too, however sometimes NextDate is not updated correctly.
I'm currently at a loss as to how to perform a delta calculation on a
field in a table that is robust and will work 100% of the time.
I'm not looking for someone to write a solution for me....but more
some help and pointers as to what could be going wrong and possible
places to look for solutions. I've combed the internet using Google
for hours and found nothing that seems any better than the examples
above.
Any help would be greatly appreciated
charlie