Solved

repeating values oracle analytics -sql

Posted on 2015-02-20
3
161 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
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 34

Accepted Solution

by:
johnsone earned 500 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

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.

Question has a verified solution.

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

Suggested Solutions

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.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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 copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

770 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