在oracle 中有张表mytest,记录是这样的:(第一列是列名)
owner   产品线      产品族     产品
test16 29-7        29-8  
test15 29-7
test16 29-7        29-8        29-9查询出:
    如果产品不为空获取产品不为空对应的那条记录的 owner,产品线,产品族,产品
    如果产品为空,则获取产品族的
    如果产品为空且产品族也为空,则获取产品线的
(相当是一个树关系  产品线 下挂 产品族 产品族 下挂 产品 )请大家帮忙看下能否写出这样的sql,  多谢!

解决方案 »

  1.   


    with t as(
    select 'test16' owner,'29-7' a,'29-8' b,'' c from dual
    union all
    select 'test15' owner,'29-7' a,'' b,'' c from dual
    union all
    select 'test16' owner,'29-7' a,'29-8' b,'29-9' c from dual
    )select t_out.owner,t_out.a,t_out.b,t_out.c
    from (
    select t.owner,t.a,t.b,t.c,row_number() over(partition by t.owner order by t.c,t.b,t.a) cn
    from t ) t_out
    where t_out.cn = 1
      

  2.   

    你的sql我拿到实际中尝试了一下: 查出来的结果是2条 不对
    select * from 
    (
    select owner,productline,productfamily,product,id,row_number() over(partition by owner order by product) cn
    from mytest ) t_out
    where t_out.cn = 1
      

  3.   

    一楼的可以吧 他有对owner进行了分组 所以有两条数据