Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ashish908
Starting Member
18 Posts |
Posted - 2005-04-28 : 13:53:37
|
| I have a table which is filled with a Network Monitoring Tool with some data. The table contains a column which stores some numeric data (every new record will have an incremented data, this data behaves like a counter)i want to run a query where i can get the difference between two consecutive data in that column. Morever, the difference should be shown as a seperate column. for e.g. if i have a table as followsID values1 20002 20303 20904 2120i should get the result asID values Difference1 2000 null2 2030 303 2090 604 2120 30Pls note the difference comes subracting the previous row value from the current row value, in case the previous row does not exist, then it should show null.Thanks for your help |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-04-28 : 14:20:35
|
If there are no missing values in the sequence of numbers in ID column, this should do it:Select a.ID, Current_Value = a.values, Prior_Value = b.values, Difference = a.values - b.valuesfrom MyTable a left join MyTable b on a.ID-1 = b.IDorder by a.ID CODO ERGO SUM |
 |
|
|
mpetanovitch
Yak Posting Veteran
52 Posts |
Posted - 2005-04-28 : 14:37:56
|
| [code]INSERT INTO #tempSELECT 1, 2000 UNION ALL SELECT 2, 2030 UNION ALL SELECT 3, 2090 UNION ALL select 4, 2120--if you can't guarantee id increments by one(note if you use and identity you can't)SELECT t.t_id, MAX(t.t_value), MAX(t.t_value) - MAX(t1.t_value) AS t_differnceFROM #temp tLEFT OUTER JOIN #temp t1 ON t.t_id > t1.t_idGROUP BY t.t_id--if you can gurantee id increments by one(manually assigning the id is the only way)SELECT t.*, t.t_value - t1.t_value AS t_differenceFROM #temp t LEFT OUTER JOIN #temp t1 ON t.t_id = t1.t_id + 1DROP TABLE #temp[/code]Mike Petanovitch |
 |
|
|
ashish908
Starting Member
18 Posts |
Posted - 2005-04-28 : 15:08:32
|
| Thanks Michael, but i cannot guarantee that the ids are incrementing by 1, the data looks like thisId Values1113417923 11296953091113418823 11297154401113419723 11297676731113420623 11297883611113421523 11298087381113422423 11298670581113423322 11298868121113424222 11299048581113425122 11299238641113426022 11299531081113426922 11300046561113427822 11300363781113428722 11301042441113429622 11301276321113430522 1130151946 |
 |
|
|
ashish908
Starting Member
18 Posts |
Posted - 2005-04-28 : 15:11:18
|
| Also, Mike how can i write this statement,------------------------------------------------------------------------------------------INSERT INTO #tempSELECT 1, 2000 UNION ALL SELECT 2, 2030 UNION ALL SELECT 3, 2090 UNION ALL select 4, 2120------------------------------------------------------------------------------------------The table contains more than 10000 rows, how could i write 10000 union all. I dont understand |
 |
|
|
nosepicker
Constraint Violating Yak Guru
366 Posts |
Posted - 2005-04-28 : 15:35:06
|
| If you create a new table (or a temp table) with a "diff" column in addition to the "id" and "values" columns, then you can also try this after populating that new table (make sure the records are in the correct order):DECLARE @diff int, @prev_value intSET @diff = 2000 -- or whatever the first value isSET @prev_value = 2000 -- or whatever the first value isUPDATE YourTable SET @diff = diff = value - @prev_value, @prev_value = value |
 |
|
|
mpetanovitch
Yak Posting Veteran
52 Posts |
Posted - 2005-04-28 : 15:51:48
|
| The #temp table is a sample table to illustrate the select statements I have...its not part of the solution.All you need is this:(but change the column names to match yours)SELECT t.t_id, MAX(t.t_value), MAX(t.t_value) - MAX(t1.t_value) AS t_differenceFROM #temp tLEFT OUTER JOIN #temp t1 ON t.t_id > t1.t_idGROUP BY t.t_idAlso Note: not sure how the performance will be on this, might be faster to use a subquery or derived table.Mike Petanovitch |
 |
|
|
|
|
|
|
|