Solved

carry forward row values

Posted on 2011-02-22
7
1,103 Views
Last Modified: 2012-06-22
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
Comment
Question by:subratoc
  • 4
  • 3
7 Comments
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
you must have a second column in order to ensure the sorting order you have displayed.

otherwise there is no guarantee the rows will be sorted, and hence carried forward the same way.
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 250 total points
Comment Utility
given a second column called "idx" that determins the ordering, you can try something like this...



SELECT   col,
         NVL(
             LAST_VALUE(NULLIF(col, 0) IGNORE NULLS)
                 OVER (ORDER BY idx ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
             col)
             carry
    FROM yourdata
ORDER BY idx
0
 

Author Comment

by:subratoc
Comment Utility
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
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
see above
0
 

Author Comment

by:subratoc
Comment Utility
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).
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
same as before except include the ordering column
ee.txt
0
 

Author Comment

by:subratoc
Comment Utility
Excellent! Thanks a ton. You are a genius indeed.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now