Solved

repeating values oracle analytics -sql

Posted on 2015-02-20
3
157 Views
Last Modified: 2015-02-20
i have the following data (select * from tbl_years):

tbl_id              tbl_class      tbl_teacher
1                     Y07               Mr Jones
2                     Y07	
3                     Y07	
4                     Y09	
5                     Y09                Mr Smith
6                     Y10                 Mr Smith
7                     Y11	
8                     Y11                  Mr Brown
9                     Y11	
10                   Y11	

Open in new window


and want the follwing output:

        
tbl_id	  tbl_class	  tbl_teacher
1                  Y07            Mr Jones
2                  Y07            Mr Jones
3                   Y07            Mr Jones
4                   Y09           Mr Smith
5                   Y09          Mr Smith
6                  Y10            Mr Smith
7                  Y11          Mr Brown
8                  Y11           Mr Brown
9                   Y11         Mr Brown
10                 Y11         Mr Brown

Open in new window


what sql syntax (analytic functtion) would i need to use to achive this result? thanks
0
Comment
Question by:tonMachine100
  • 2
3 Comments
 
LVL 34

Expert Comment

by:johnsone
Comment Utility
Since you need to look back and look forward to determine the teacher, I'm not sure you can do it with an analytic function.  Maybe something complicated with multiple analytics, but this gives the result you are asking for.
SELECT a.tbl_id, 
       a.tbl_class, 
       Nvl(a.tbl_teacher, b.tbl_teacher) tbl_teacher 
FROM   tbl_years a 
       left outer join (SELECT tbl_class, 
                               Min(tbl_teacher) tbl_teacher 
                        FROM   tbl_years 
                        GROUP  BY tbl_class) b 
                    ON a.tbl_class = b.tbl_class 
ORDER  BY a.tbl_id; 

Open in new window

0
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
Comment Utility
I wasn't thinking of the correct partitioning.  I was looking at teacher and id, and not looking at class.  You can do it with an analytic function this way:
SELECT a.tbl_id, 
       a.tbl_class, 
       Last_value(a.tbl_teacher ignore nulls) 
         over ( 
           PARTITION BY tbl_class 
           ORDER BY tbl_teacher nulls last) tbl_teacher 
FROM   tbl_years a 
ORDER  BY a.tbl_id; 

Open in new window

0
 

Author Closing Comment

by:tonMachine100
Comment Utility
great thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

771 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

7 Experts available now in Live!

Get 1:1 Help Now