Link to home
Start Free TrialLog in
Avatar of tonMachine100
tonMachine100

asked on

repeating values oracle analytics -sql

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
Avatar of johnsone
johnsone
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of johnsone
johnsone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tonMachine100
tonMachine100

ASKER

great thanks