Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Evaluating Previous Row Value to Current Row Value and Performing Action

$
0
0

I need to perform a series of evaluations and then do an update on fields if they are true. The evaluation is performed on a single table against the current row and the previous row. 

I'll try to show this through an example. Below is the table with the fields and some data as well as what the results should be.

Field1	Field2	Field3	Field4	Field5	FirstEntry	LastEntry
A	B	B	B	B	Yes		Yes
B	B	B	B	B
B	B	B	B	B			Yes
B	B	B	C	B
B	B	B	C	B			Yes	
B	B	B	C	D			

I'll try to show this logic through some psuedo code with If statements such as below. (Variables beginning with Pre are the previous rows value)

If PreField1 <>	Field1 
	Or PreField2 <> Field2
	Or PreField3 <> Field3 Then
	PreFirstEntry = Yes
	PreLastEntry = Yes	
ElseIF PreField4 <> Field4 Then
	PreFirstEntry = Yes
ElseIf PreField5 <> Field5 Then
	PreFirstEntry = Yes

I hope that makes sense. This is currently written in VBA. If it would make it easier to understand if I just C&Ped the existing VBA code in here, let me know. Thanks. 




Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>