Calculating Differences Between Successive Rows : Join Table « Join « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Join » Join Table 
Calculating Differences Between Successive Rows
      
mysql>
mysql>
mysql> CREATE TABLE trip_log
    -> (
    ->  seq             INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  city    VARCHAR(30NOT NULL,   # location of stop
    ->  t               DATETIME NOT NULL,              # time of stop
    ->  miles   INT UNSIGNED NOT NULL,  # miles traveled so far
    ->  fuel    DECIMAL(6,3),                   # gas used between citees
    ->  PRIMARY KEY (seq)
    -> );
Query OK, rows affected (0.00 sec)

mysql>
mysql> INSERT INTO trip_log (t, city, miles, fuel)
    -> VALUES
    ->  ('2010-10-23 05:00:00', 'San Antonio, TX', 00),
    ->  ('2010-10-23 09:10:00', 'Dallas, TX', 26311.375),
    ->  ('2010-10-23 13:40:00', 'Benton, AR', 56612.398),
    ->  ('2010-10-23 16:51:00', 'Memphis, TN', 7456.820),
    ->  ('2010-10-23 19:06:00', 'Portageville, MO', 745+1337.007),
    ->  ('2010-10-23 23:16:00', 'Champaign, IL', 745+41911.354),
    ->  ('2010-10-24 03:27:00', 'Madison, WI', 745+66713.016)
    -> ;
Query OK, rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT FROM trip_log;
+-----+------------------+---------------------+-------+--------+
| seq | city             | t                   | miles | fuel   |
+-----+------------------+---------------------+-------+--------+
|   | San Antonio, TX  | 2010-10-23 05:00:00 |     |  0.000 |
|   | Dallas, TX       | 2010-10-23 09:10:00 |   263 11.375 |
|   | Benton, AR       | 2010-10-23 13:40:00 |   566 12.398 |
|   | Memphis, TN      | 2010-10-23 16:51:00 |   745 |  6.820 |
|   | Portageville, MO | 2010-10-23 19:06:00 |   878 |  7.007 |
|   | Champaign, IL    | 2010-10-23 23:16:00 |  1164 11.354 |
|   | Madison, WI      | 2010-10-24 03:27:00 |  1412 13.016 |
+-----+------------------+---------------------+-------+--------+
rows in set (0.00 sec)

mysql>
mysql> SELECT t1.seq AS seq1, t2.seq AS seq2,
    -> t1.city AS city1, t2.city AS city2,
    -> t1.miles AS miles1, t2.miles AS miles2,
    -> t2.miles-t1.miles AS dist
    -> FROM trip_log AS t1, trip_log AS t2
    -> WHERE t1.seq+= t2.seq
    -> ORDER BY t1.seq;
+------+------+------------------+------------------+--------+--------+------+
| seq1 | seq2 | city1            | city2            | miles1 | miles2 | dist |
+------+------+------------------+------------------+--------+--------+------+
|    |    | San Antonio, TX  | Dallas, TX       |      |    263 |  263 |
|    |    | Dallas, TX       | Benton, AR       |    263 |    566 |  303 |
|    |    | Benton, AR       | Memphis, TN      |    566 |    745 |  179 |
|    |    | Memphis, TN      | Portageville, MO |    745 |    878 |  133 |
|    |    | Portageville, MO | Champaign, IL    |    878 |   1164 |  286 |
|    |    | Champaign, IL    | Madison, WI      |   1164 |   1412 |  248 |
+------+------+------------------+------------------+--------+--------+------+
rows in set (0.00 sec)

mysql>
mysql>
mysql> drop table trip_log;
Query OK, rows affected (0.00 sec)

   
    
    
    
    
    
  
Related examples in the same category
1.NATURAL JOIN in MySQL
2.Two NATURAL JOIN in select command
3.The most expensive book per author
4.Shows the daily and cumulative precipitation for each day
5.display each part number, name and price
6.Display the all members of staff in the same department as Tony West
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.