[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

repeating values oracle analytics -sql

Posted on 2015-02-20
3
Medium Priority
?
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Expert Comment

by:johnsone
ID: 40620990
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 35

Accepted Solution

by:
johnsone earned 2000 total points
ID: 40621000
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
ID: 40621196
great thanks
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database

649 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