The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
calculating in MB cesarNZ Shell Programming and Scripting 3 09-14-2009 11:00 PM
Bash executing Orcale Update statement satnamx UNIX for Advanced & Expert Users 1 07-06-2009 04:44 AM
calculating a number stonemonolith Shell Programming and Scripting 2 09-05-2008 04:38 PM
How is use sselect statement o/p in insert statement. nkosaraju Shell Programming and Scripting 2 08-06-2008 10:26 PM
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 02:14 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-14-2009
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 267
UPDATE statement: calculating changes

The problem I was working on is solved, but felt it would be worthwhile to ask for some opinions as to whether the approach can actually be improved.
I am using the following example and data taken from SQL datetime calculations and MySQL 5.1:

Code:
CREATE TABLE `data` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `datetime` datetime NOT NULL,
  `temperature` float NOT NULL,
  `change` float NOT NULL,
  PRIMARY KEY (id)) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `data` (`datetime`, `temperature`) VALUES ('2009-01-01 06:07:08','70.4')
,('2009-01-01 06:08:08','62.1')
,('2009-01-01 06:09:08','78.8')
,('2009-01-01 07:08:09','72.6')
,('2009-01-01 07:08:10','63.2')
,('2009-01-01 07:08:11','79.9')
,('2009-01-01 08:09:10','69.3')
,('2009-01-01 11:12:13','64.3')
;
Now, to calculate the changes in temperature, the following query can be used:
Code:
UPDATE data AS D1, data AS D2 SET D1.change = D1.temperature - D2.temperature WHERE D1.id = D2.id + 1;
This works as designed, but requires the need for the data to be sorted first by datetime and - for performance reasons - the addition of an auto-increment called id. Perhaps there are other approaches, which are used commonly in situations where changes in data need to be calculated?
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0