Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

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
0
tonMachine100
Asked:
tonMachine100
  • 2
1 Solution
 
johnsoneSenior Oracle DBACommented:
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
 
johnsoneSenior Oracle DBACommented:
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
 
tonMachine100Author Commented:
great thanks
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now