Home » SQL & PL/SQL » SQL & PL/SQL » Difference between first reading and second reading? (SQL)
Difference between first reading and second reading? [message #375831] Mon, 15 December 2008 01:41 Go to next message
arunsuresh100
Messages: 38
Registered: December 2006
Member
Hi,

Meteorological department recorded the temperature on daily basis. The table data would be like this

Date	Celsius(Temperature)
2008-12-07	40
2008-12-08	45
2008-12-09	35
2008-12-10	37
2008-12-11	50


We need to find the difference of temperature between today & yesterday. The sample result given below:

Date	Temperature Difference
2008-12-08	5
2008-12-09	-10
2008-12-10	2
2008-12-11	13


Please provide solution using SQL queries.
Re: Difference between first reading and second reading? [message #375832 is a reply to message #375831] Mon, 15 December 2008 01:57 Go to previous messageGo to next message
Michel Cadot
Messages: 64111
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code (See SQL Formatter) and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version (4 decimals).

Post a test case: create table and insert statements along with the result you want with these data.

Have a look at LAG/LEAD functions.

Regards
Michel
Re: Difference between first reading and second reading? [message #375836 is a reply to message #375832] Mon, 15 December 2008 02:10 Go to previous message
Frank Naude
Messages: 4502
Registered: April 1998
Senior Member
As Michel suggested, LAG will do the job:

SQL> SELECT d, t - LAG(t) OVER (ORDER BY d) FROM x;

D         T-LAG(T)OVER(ORDERBYD)
--------- ----------------------
07-DEC-08
08-DEC-08                      5
09-DEC-08                    -10
10-DEC-08                      2
11-DEC-08                     13

Previous Topic: problem in inserting as well as selecting
Next Topic: duplicate rows
Goto Forum:
  


Current Time: Tue Dec 06 00:02:57 CST 2016

Total time taken to generate the page: 0.06013 seconds