473,657 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_emit ted 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 13773
dm****@gmail.co m wrote in
news:11******** **************@ u72g2000cwu.goo glegroups.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_emit ted 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_redu ced - (SELECT last(tons_reduc ed)
FROM table ALIAS Subtable
WHERE subtable.Contro l_ID = table.control_i d
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_redu ced - (SELECT last(tons_reduc ed)
FROM table ALIAS Subtable
WHERE subtable.Contro l_ID = table.control_i d
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_redu ced - (SELECT TOP 1 tons_reduced
FROM table ALIAS Subtable
WHERE subtable.Contro l_ID = table.control_i d
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**********@S PAMyahoo.com> wrote in
news:RB******** *********@torna do.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_redu ced - (SELECT last(tons_reduc ed)
FROM table ALIAS Subtable
WHERE subtable.Contro l_ID = table.control_i d
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_redu ced - (SELECT TOP 1 tons_reduced
FROM table ALIAS Subtable
WHERE subtable.Contro l_ID = table.control_i d
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
4397
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: starttime = Date.parse("Aug 10,2003, 07:07") sdt = new Date(starttime)
5
2158
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 10/21/1993
4
31202
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 11/3/2005 21:05:35 1131051935.20 2 3 5 1009 1043
1
2092
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 the percent changes of sales from year to year
5
19813
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 monthDifference(DateTime startDate, DateTime endDate) { int monthsApart = 12 * (startDate.Year - endDate.Year) +
7
8667
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 beween the current and preceeding result for that fortnighlty wage. eg 1st wage = 1000 diff = 0 (as this is first entry) 2nd wage = 1500 diff = 500 3rd wage = 1750 diff = 250 and so on. this is driving me nuts so...
0
2392
by: shilpa.vastrad | last post by:
how to calculate rank in selected records in sql server 2000
6
3414
by: lenygold via DBMonster.com | last post by:
Here is my input table: TUE MON ----------- ----------- 2 - - 25 27 - - 48 50 - - 78
7
12033
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 record. this should be a continious excercise in ms access database table
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.