473,387 Members | 1,844 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,387 software developers and data experts.

cumulative 'difference'

Hi All,

I have a situation here in mysql:
The table desc is:

Field Type Null Key Default Extra
-------- ------- ------- ------ ---------- --------
dn_date date PRI 0000-00-00
dn int(10) PRI 0
ctr1 int(10) YES (null)
ctr2 int(10) YES (null)
ctr3 int(10) YES (null)
ctr4 int(10) YES (null)
isActive char(3)

Now i want this kind of result

For one given DN value and date range, the expected output which i want
is :

FMR for the DN: 2280000
CTR1+CTR2 Difference in readings
31/12/2004 X1 --
15/01/2005 X2 X2-x1
31/01/2005 X3 X3-x2
15/02/2005 X4 X4-x3
28/02/2005 X5 X5-x4

And so on..

That is user enters a range of date and a dn value and he gets the
above output.
Thanks for any suggestions/directions

Sachin

Jul 23 '05 #1
1 1912
sa***********@gmail.com wrote:
Hi All,

I have a situation here in mysql:
The table desc is:

Field Type Null Key Default Extra
-------- ------- ------- ------ ---------- --------
dn_date date PRI 0000-00-00
dn int(10) PRI 0
ctr1 int(10) YES (null)
ctr2 int(10) YES (null)
ctr3 int(10) YES (null)
ctr4 int(10) YES (null)
isActive char(3)

Now i want this kind of result

For one given DN value and date range, the expected output which i want
is :

FMR for the DN: 2280000
CTR1+CTR2 Difference in readings
31/12/2004 X1 --
15/01/2005 X2 X2-x1
31/01/2005 X3 X3-x2
15/02/2005 X4 X4-x3
28/02/2005 X5 X5-x4

And so on..

That is user enters a range of date and a dn value and he gets the
above output.
Thanks for any suggestions/directions


In general, anytime you need to compare or calculate values from
different records of a table, you need to do a self-join.

Also, you need to do something tricky to find the "previous" record,
since they increment by an irregular pattern. That is, you can't just
subtract 15 days from one record's dn_date to find the previous record.
Try something like this (though it is untested):

SELECT d1.dn_date, d1.crt1 + d1.ctr2AS X,
(d1.crt1 + d1.crt2) - (d2.crt1 + d2.crt2) AS difference_in_readings
FROM desc AS d1 LEFT OUTER JOIN desc AS d2
ON d2.dn_date = (
SELECT MAX(ds.dn_date) FROM desc AS ds
WHERE ds.dn_date >= '$user_min_date' AND ds.dn_date < d1.dn_date)
WHERE d1.dn_date BETWEEN '$user_min_date' AND '$user_max_date'

Regards,
Bill K.
Jul 23 '05 #2

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

Similar topics

4
by: Yannick Turgeon | last post by:
Hello all, I've got a query which suddently became very slow. It now takes about 10 secs instead of 2 secs. I've got to identical DB (one is for test and the other is production). The query...
4
by: Matt Larkin | last post by:
I am pulling my hair out trying to solve this one (presumably because I am not particularly trained or skilled at access!) I have a query which summarises the variances that each of my sales...
1
by: Victor | last post by:
Hi There, I have a query witch gives me the following result: Period NumberOfItems 1 13 2 2 3 1 4 1 5 1
4
by: cefrancke | last post by:
Are there any ways to speed up a Cumulative sum in a query or perhaps another faster way to have a cumulative sum column (call a vba function?). For example, I want to sum up all values under...
0
by: James Hallam | last post by:
I have searched through the news groups and found many threads close to what I want but cannot get any of them to work... I have a table with expenses and invoices, what I want is a bar chart...
3
ChaseCox
by: ChaseCox | last post by:
Hi all, I have a problem that I have been looking at for a couple days now and I can not quite get it to work. I would like to calculate the cumulative percent failure of a certain product in...
1
by: wisemen | last post by:
I have a table with 2 columns, and .I want to run a query that will give me a cumulative sum of the no. of entries that have an <= and on the second column give me a cumulative sum of the no. of...
1
by: cbellew | last post by:
Hi guys, i'm looking to create a report with a table showing totals (running and cumulative) of education sessions attend by the staff at a hospital. I'm trying to get the table to show something...
4
by: JAG | last post by:
The following line of code worked in my .hta prior to installing MS08-045 - Cumulative Security Update for Internet Explorer (953838) (on both XP and W2K): window.top.frames.location = ; After...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.