subratoc
asked on
carry forward row values
I have got table with 10 rows and one column:
0
0
1
0
0
0
2
0
0
0
I want to write a select query which will carry forward any non-zero values to successive rows unless the value in the column changes.
This means that the query will return the following for the data shown above:
0 0
0 0
1 1 --value changed from 0 to 1 in column 1
0 1 --value '1' carried forward as the current value of column1 is zero
0 1 --value '1' carried forward as the current value of column1 is zero
0 1 --value '1' carried forward as the current value of column1 is zero
2 2 --value '1' not carried forward as the current value of column1 is 2
0 2 --value '1' carried forward as the current value of column1 is zero
0 2 --value '1' carried forward as the current value of column1 is zero
0 2 --value '1' carried forward as the current value of column1 is zero
Any idea anybody?
Thanks in advance.
0
0
1
0
0
0
2
0
0
0
I want to write a select query which will carry forward any non-zero values to successive rows unless the value in the column changes.
This means that the query will return the following for the data shown above:
0 0
0 0
1 1 --value changed from 0 to 1 in column 1
0 1 --value '1' carried forward as the current value of column1 is zero
0 1 --value '1' carried forward as the current value of column1 is zero
0 1 --value '1' carried forward as the current value of column1 is zero
2 2 --value '1' not carried forward as the current value of column1 is 2
0 2 --value '1' carried forward as the current value of column1 is zero
0 2 --value '1' carried forward as the current value of column1 is zero
0 2 --value '1' carried forward as the current value of column1 is zero
Any idea anybody?
Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK. Let's say I have another column (not shown above) which ensures the order of the rows.
This revises the question to:
The query shouldreturn the following for the data shown above:
0 0
0 0
1 1 --value changed from 0 to 1 in column 1
0 1 --value '1' carried forward as the current value of column1 is zero
0 1 --value '1' carried forward as the current value of column1 is zero
0 1 --value '1' carried forward as the current value of column1 is zero
2 2 --value '1' not carried forward as the current value of column1 is 2
0 2 --value '2' carried forward as the current value of column1 is zero
0 2 --value '2' carried forward as the current value of column1 is zero
0 2 --value '2' carried forward as the current value of column1 is zero
This revises the question to:
The query shouldreturn the following for the data shown above:
0 0
0 0
1 1 --value changed from 0 to 1 in column 1
0 1 --value '1' carried forward as the current value of column1 is zero
0 1 --value '1' carried forward as the current value of column1 is zero
0 1 --value '1' carried forward as the current value of column1 is zero
2 2 --value '1' not carried forward as the current value of column1 is 2
0 2 --value '2' carried forward as the current value of column1 is zero
0 2 --value '2' carried forward as the current value of column1 is zero
0 2 --value '2' carried forward as the current value of column1 is zero
see above
ASKER
Please ignore the last post. Revised question is:
OK. Let's say I have another column (not shown above) which ensures the order of the rows.
This revises the question to:
The query shouldreturn the following for the data shown above:
0 0 1
0 0 2
1 1 3 --value changed from 0 to 1 in column 1
0 1 4 --value '1' carried forward as the current value of column1 is zero
0 1 5 --value '1' carried forward as the current value of column1 is zero
0 1 6 --value '1' carried forward as the current value of column1 is zero
2 2 7 --value '1' not carried forward as the current value of column1 is 2
0 2 8 --value '2' carried forward as the current value of column1 is zero
0 2 9 --value '2' carried forward as the current value of column1 is zero
0 2 10 --value '2' carried forward as the current value of column1 is zero
Here I am ensuring the order using the third column (1-10).
OK. Let's say I have another column (not shown above) which ensures the order of the rows.
This revises the question to:
The query shouldreturn the following for the data shown above:
0 0 1
0 0 2
1 1 3 --value changed from 0 to 1 in column 1
0 1 4 --value '1' carried forward as the current value of column1 is zero
0 1 5 --value '1' carried forward as the current value of column1 is zero
0 1 6 --value '1' carried forward as the current value of column1 is zero
2 2 7 --value '1' not carried forward as the current value of column1 is 2
0 2 8 --value '2' carried forward as the current value of column1 is zero
0 2 9 --value '2' carried forward as the current value of column1 is zero
0 2 10 --value '2' carried forward as the current value of column1 is zero
Here I am ensuring the order using the third column (1-10).
same as before except include the ordering column
ee.txt
ee.txt
ASKER
Excellent! Thanks a ton. You are a genius indeed.
otherwise there is no guarantee the rows will be sorted, and hence carried forward the same way.