[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
Medium Priority
169 Views
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
``````

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
``````

what sql syntax (analytic functtion) would i need to use to achive this result? thanks
0
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
• 2

LVL 35

Expert Comment

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;
``````
0

LVL 35

Accepted Solution

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;
``````
0

Author Closing Comment

ID: 40621196
great thanks
0

## Featured Post

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…