473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to calculate the difference between values in preceeding records

I have data on air pollution coming from sources across the country,
and control options for reducing pollution from each source. The
example below shows data for:
- 2 sources (plant_ID "001" and "008")
- total tons of pollution emitted from each source (20 and 25 tons,
respectively)
- an ID code for the each control device that could be applied to
reduce the emissions
- the control efficiency for each control device (percent that it
would reduce the emissions)
- the tons that each control device would reduce the emissions

I need to create an Access query that will calculate the incremental
tons reduced ("inc_red"), i.e., the difference between the tons reduced
by the device listed in the current record minus the tons reduced by
the device in the previous record, if the previous record is for the
same plant. Eg., the 1st control device is listed in record # 2 and it
would reduce emissions a total of 10 tons. The 2nd control device is
listed in record # 3 and it would reduce emissions a total of 10 tons.
The incremental difference is calculated by subtracting the reduction
for record # 2 from the reduction for record # 3, which equals zero
(because they are both 10). I have greatly simplified the problem, but
this gets at the essence of my problem. I have a database of over
100,000 records for which I need to do similar calculations, and the
database is updated regularly.

ID plant_ID total_tons_emitted control_ID CE tons_reduced
inc_red
1 001 20 no control
2 001 20 N01 0.5 10 10
3 001 20 N08 0.5 10 0
4 001 20 N03 0.8 16 6
5 008 25 no control
6 008 25 N04 0.4 10 10
7 008 25 N07 0.6 15 5

Feb 25 '06 #1
4 13759
dm****@gmail.com wrote in
news:11**********************@u72g2000cwu.googlegr oups.com:
I have data on air pollution coming from sources across the
country, and control options for reducing pollution from each
source. The example below shows data for:
- 2 sources (plant_ID "001" and "008")
- total tons of pollution emitted from each source (20 and 25
tons,
respectively)
- an ID code for the each control device that could be
applied to
reduce the emissions
- the control efficiency for each control device (percent
that it
would reduce the emissions)
- the tons that each control device would reduce the
emissions

I need to create an Access query that will calculate the
incremental tons reduced ("inc_red"), i.e., the difference
between the tons reduced by the device listed in the current
record minus the tons reduced by the device in the previous
record, if the previous record is for the same plant. Eg., the
1st control device is listed in record # 2 and it would reduce
emissions a total of 10 tons. The 2nd control device is listed
in record # 3 and it would reduce emissions a total of 10
tons. The incremental difference is calculated by subtracting
the reduction for record # 2 from the reduction for record #
3, which equals zero (because they are both 10). I have
greatly simplified the problem, but this gets at the essence
of my problem. I have a database of over 100,000 records for
which I need to do similar calculations, and the database is
updated regularly.

ID plant_ID total_tons_emitted control_ID CE
tons_reduced inc_red
1 001 20 no control
2 001 20 N01 0.5 10
10 3 001 20 N08 0.5 10
0 4 001 20 N03 0.8
16 6 5 008 25 no control
6 008 25 N04 0.4 10
10 7 008 25 N07 0.6 15
5

You could use a subquery to return the preceding record, based
on matching plant_ID, Control_ID, and smaller ID or entrydate.

SELECT * from table,
table.Tons_reduced - (SELECT last(tons_reduced)
FROM table ALIAS Subtable
WHERE subtable.Control_ID = table.control_id
AND subtable.plant_ID = table.plant_ID
AND subtable.ID < table.ID) as inc_red
FROM table;

-
Bob Quintal

PA is y I've altered my email address.
Feb 25 '06 #2
> You could use a subquery to return the preceding record, based
on matching plant_ID, Control_ID, and smaller ID or entrydate.

SELECT * from table,
table.Tons_reduced - (SELECT last(tons_reduced)
FROM table ALIAS Subtable
WHERE subtable.Control_ID = table.control_id
AND subtable.plant_ID = table.plant_ID
AND subtable.ID < table.ID) as inc_red
FROM table;


Unless I misunderstood the post I don't think this is going to
work for a couple of reasons. The Last() function doesn't give
you the last record and without an ORDER BY clause the order
returned by the database is undefined and cannot be depending on.

Instead a solution is to look for the previous value with a
SELECT TOP 1 query (which is not portable to other DBMS systems -
it is a JET only feature).

SELECT * from table As t,
table.Tons_reduced - (SELECT TOP 1 tons_reduced
FROM table ALIAS Subtable
WHERE subtable.Control_ID = table.control_id
AND subtable.plant_ID = table.plant_ID
AND subtable.ID < t.ID
ORDER BY subtable.ID Desc) as inc_red
FROM table;

The Tons_reduced field could perhaps be substituted in the
subquery's "AND subtable.ID < t.ID" instead of ID.

--
'---------------
'John Mishefske
'---------------
Feb 26 '06 #3
John Mishefske wrote:
Instead a solution is to look for the previous value with a
SELECT TOP 1 query (which is not portable to other DBMS systems -
it is a JET only feature). From Ms-SQL Books on Line:


"You can use the TOP clause to limit the number of rows that are
returned in the result set.

TOP ( expression ) [ PERCENT ] [ WITH TIES ]

expression is a numeric expression that specifies the number of rows to
be returned; or if PERCENT is specified, the percentage (specified by
expression) of the result set rows is returned. For example:

TOP (120) /*Return the top 120 rows of the result set. */
TOP (15) PERCENT /* Return the top 15 percent of the result set. */.
TOP(@n) /* Return the top @n rows of the result set, with the variable
declaration: DECLARE @n AS BIGINT; SET @n = 2 */."

Feb 26 '06 #4
John Mishefske <jm**********@SPAMyahoo.com> wrote in
news:RB*****************@tornado.rdc-kc.rr.com:
You could use a subquery to return the preceding record, based on matching plant_ID, Control_ID, and smaller ID or entrydate.
SELECT * from table,
table.Tons_reduced - (SELECT last(tons_reduced)
FROM table ALIAS Subtable
WHERE subtable.Control_ID = table.control_id
AND subtable.plant_ID = table.plant_ID
AND subtable.ID < table.ID) as inc_red
FROM table;
Unless I misunderstood the post I don't think this is going to
work for a couple of reasons. The Last() function doesn't give
you the last record and without an ORDER BY clause the order
returned by the database is undefined and cannot be depending

on.
Instead a solution is to look for the previous value with a
SELECT TOP 1 query (which is not portable to other DBMS systems - it is a JET only feature).

SELECT * from table As t,
table.Tons_reduced - (SELECT TOP 1 tons_reduced
FROM table ALIAS Subtable
WHERE subtable.Control_ID = table.control_id
AND subtable.plant_ID = table.plant_ID
AND subtable.ID < t.ID
ORDER BY subtable.ID Desc) as inc_red
FROM table;

The Tons_reduced field could perhaps be substituted in the
subquery's "AND subtable.ID < t.ID" instead of ID.


Interesting observation: you correctly picked up I had an error
in my code, but offered a different correction, then a third
variant on the theme..

I typed last() instead of the intended max()

Again we prove that there's more than one way...

--
Bob Quintal

PA is y I've altered my email address.
Feb 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
5
by: tamilan71 | last post by:
Hello All I have table with following fields: GroupId VisitDate 1 10/19/1993 1 11/24/1998 2 10/18/1993 2 10/29/1998 3 ...
4
by: iamonthisboat | last post by:
I have a data set like so: UTC_TIME Timestamp NodeID Message Flag Line Station 11/19/2005 10:45:07 1132397107.91 1 3 5 1028 1034...
1
by: cecil | last post by:
I have filed titled "Sales". My report list the following records: Year Sales 2002 1,000 2003 5.000 2004 6,000 2005 7,000 I would like to calculate...
5
by: Mike | last post by:
I use c#, V2005 How I can get difference between two dates and get value in month(s) I had found some solutions but it is not exactly what I need. private static int...
7
by: jpharps | last post by:
I have query that calculates pay based on several factors then returns a figure for a fortnightly wage. I'm trying to work out a way to have a running sum in that query that will show the difference...
0
by: shilpa.vastrad | last post by:
how to calculate rank in selected records in sql server 2000
6
by: lenygold via DBMonster.com | last post by:
Here is my input table: TUE MON ----------- ----------- 2 - - 25 27 - - 48 50 - - 78
7
by: orajat | last post by:
hi, how do i calculate difference in time in seconds (00:09:05 - 00:09:00 = 300) where start time being a field and the difference in time is calculated in AHT field, ie last record minus previous...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.