Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How do I go about getting the difference between the previous cell and current cell and then put the answer in another cell in another column?

I have a CSV file that will eventually get uploaded into an Access table. I would like to do the work in a datatable before pushing it to the Access table.

Here's what I would have

columnAcolumnB
20
5030
6010
45-15


ColumnB shows the difference between the rows with the previous row.

Here's a little more to the question.

In columnA we see 20 and then below it we see 50. Beside the 50 I want it to be the difference of the 50 and 20 showing in ColumnB 30.

In the next series we see 50 and the 60 below it in ColumnA. Besides the 60 I want it to show the difference between 50 and 60 thus adding 10 to ColumnB.

Lastly, we see 60 and then 45 below it thus giving us the difference between 60 and 45 which is -15.

Basically I have a tonne of coordinates that I need to know the difference between each one so I can do other calculations based on the differences of the point before.

Would someone please point me in the right direction?

Thanks

Mike
Posted
Updated 7-Oct-12 13:05pm
v2
Comments
Thilina Chandima 6-Oct-12 22:37pm    
please explain more. what you really want.. I mean do you wan to get to columns difference or two rows difference...?
Sandeep Mewara 7-Oct-12 1:33am    
Example says, two rows difference of a given column. Here, columnB values are based on columnA value - they are difference of current & previous values.
Herman<T>.Instance 7-Oct-12 2:26am    
Is there an ID column or datetime column that can be used which can be used in an Order by but the values stay in the order given? Just the numbers is too small for CTE, Row_Number() and query.
Thilina Chandima 7-Oct-12 2:55am    
why can't you just use a loop and do the calculation...? is it impossible to do that way...?
mzrax 7-Oct-12 19:27pm    
I know that I have to loop and then do a calculation but I don't know how. Can you point me in the right direction or do you have an example??

May not work if you got repeating items, but definitely a point where you can build up.
SQL
SELECT X.ColA, ISNULL(Y.ColA,0)-X.ColA
FROM
SELECT ColA, ROW_NUMBER() OVER(ORDER BY ColA) AS RID
FROM TableA
)AS X
LEFT OUTER JOIN 
(
SELECT ColA, (1 + ROW_NUMBER() OVER(ORDER BY ColA)) AS RID
FROM TableA
)AS Y
ON X.RID = Y.RID 
 
Share this answer
 
Comments
damodara naidu betha 8-Oct-12 2:33am    
Good one.But this needs be corrected. To get difference, replace ISNULL(Y.ColA,0)-X.ColA With ISNULL(X.ColA,0)-Y.ColA, and you are taking row number order by ColA. This sorts the column values in ascending order. So the query doesn't produce desired result.
mzrax 8-Oct-12 22:58pm    
I have to say your solution does the trick too. Again, I'm stumped because I thought I was going to take my project in a very specific way and now that both of you have given excellent solutions I'm going to have to rethink everything.



I just wanted to say that I added an ID column and did the order by the ID and therefore I shouldn't have to worry about duplicates.

I wanted to thank you too and a big thumbs up. Now I have to figure out how to integrate this with my C# project.



Thanks
M
Hi Mike...

Try this code block ...

SQL
  Declare @MyTestTable table
(
id Int,
PointValue int
)

--—Insert sample data
-- NOTE : Here Id should  be unique and in order.

Insert into @MyTestTable Values (1,150)
Insert into @MyTestTable Values (2,350)
Insert into @MyTestTable Values (3,500)
Insert into @MyTestTable Values (4,100)
Insert into @MyTestTable Values (5,150);

--Create CTE
With tblDifference as
(
Select Row_Number() OVER (Order by id) as RowNumber,id,PointValue from @MyTestTable
)

--Actual Query
Select Cur.PointValue as CurrentValue, ISNULL(Prv.PointValue,0) as PreviousValue,ISNULL(Cur.PointValue-Prv.PointValue,0) as Difference from
tblDifference Cur Left Outer Join tblDifference Prv
On Cur.RowNumber=Prv.RowNumber+1
Order by Cur.id
 
Share this answer
 
Comments
Herman<T>.Instance 8-Oct-12 8:55am    
taken from the internet via this article
Kuthuparakkal 8-Oct-12 11:20am    
my 5+
mzrax 8-Oct-12 22:33pm    
This is exactly what I was looking for and now that I see it I can't believe I didn't think about it.

Here's the question that I have. Is there a way to do this with C# and update the table? Would I use a foreach loop? I was hoping to do this with more C# but using SQL has definitely opened the door to another possibility. Because there's so much more to my problem and what I'm trying to do, you solution has definitly produced more questions than answers in my head.

The one thing I do have to say is a big thanks. Big thumbs up.

Thanks
M

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900