获取行的值并将其显示在列中

时间:2015-09-12 21:57:25

标签: sql oracle plsql oracle11g

我有一个包含一些数据的xml文件,我使用ETL(Oracle数据集成器)将其加载到表(Oracle数据库)中。

该表包含以下数据

id     student      class      scoreT1         scoreT2 
1      English       3-B       null             null
2      null          null      12/09/2014       18/09/2014
3      null          null        null            null
4      Jack          null        15.5             18
5      Richard       null        16               17
6      Jhon          null        17               14
7      French        5-B        null             null
8      null         null       12/09/2014       18/09/2014 
9      null         null        null            null
10     Cena         null        14.5            13
11     Mike         null        12              17
12     Philip       null        10              14

我需要一个select查询来获得如下结果:

   id     student      class      scoreT1         scoreT2             val1         val2            val3          val4
    1      English       3-B       null             null              3-B       12/09/2014       18/09/2014     English 
    2      null          null      12/09/2014       18/09/2014        3-B       12/09/2014       18/09/2014     English
    3      null          null        null            null             3-B       12/09/2014       18/09/2014     English
    4      Jack          null        15.5             18              3-B       12/09/2014       18/09/2014     English
    5      Richard       null        16               17              3-B       12/09/2014       18/09/2014     English
    6      Jhon          null        17               14              3-B       12/09/2014       18/09/2014     English
    7      French        5-B        null             null             5-B       12/09/2014       18/09/2014     French
    8      null         null       12/09/2014       18/09/2014        5-B       12/09/2014       18/09/2014     French
    9      null         null        null            null              5-B       12/09/2014       18/09/2014     French
    10     Cena         null        14.5            13                5-B       12/09/2014       18/09/2014     French
    11     Mike         null        12              17                5-B       12/09/2014       18/09/2014     French
    12     Philip       null        10              14                5-B       12/09/2014       18/09/2014     French

我需要SQL语句或PL/SQL才能获得结果我不知道是否可能

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

试试我的解决方案:

with cte as (
      select 
        id,student,class,scoreT1,scoreT2,
        sum (case when class is not null then 1 else 0 end ) over (order by id) class_flag1,
        sum (case when class is not null then 0 else 1 end ) over (order by id) class_flag2,
        sum (case when scoreT1 like '__/__/____' then 1 else 0 end ) over (order by id) scoreT1_flag1,
        sum (case when scoreT1 like '__/__/____' then 0 else 1 end ) over (order by id) scoreT1_flag2
      from tab
    )
    select 
      a.id,a.student,a.class,a.scoreT1,a.scoreT2,
      nvl(first_value(a.class) over (partition by id - a.class_flag2 order by id),b.class) class_new,
      nvl(first_value(a.scoreT1) over (partition by id - a.scoreT1_flag2 order by id),c.scoreT1) scoreT1_new
    from cte a
    cross join (select min(class) keep (dense_rank first order by id) class from cte where class_flag1 = 1) b
    cross join (select min(scoreT1) keep (dense_rank first order by id) scoreT1 from cte where scoreT1_flag1 = 1) c

答案 1 :(得分:0)

您可以使用val1使用ignore nulls获取lag()。很难看出最后一列的逻辑是什么。但是,我猜你正在寻找T1分数的特定格式。所以,这可能会做你想要的:

select max(case when scoreT1 like '__/__/____' then scoreT1 end) over (partition by val1) as val2
from (select lag(class ignore nulls) over (order by id) as val1
      . . .
     ) t;