Oracle SQL:在一行返回结果

时间:2015-06-09 08:00:30

标签: sql oracle

我可以在一行中显示以下结果吗?

目前,CASE_COMPONENT_PART,CASE_DESCRIPTION和CASE_PROD与TRAY_COMPONENT_PART,TRAY_DESCRIPTION和TRAY_PROD位于不同的行。

结果是否都出现在同一行?

select li.cunr, oc.name cust_name, li.catnr fp_catnr, li.av_part_no fp_part_no, 
trim(li.artist||' '||li.title) fp_description,
(case when substr(li1.prodtyp,1,2) = 'JC' then 
(case 
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null 
            then (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end)
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null and  
        (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end) is null
            then (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then ps2.component_part else '' end)
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null and  
        (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end) is null and 
        (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then ps2.component_part else '' end) is null
            then (case when substr(pd3.prodtyp,1,2) in ('JC','TR') then ps3.component_part else '' end)
    else (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end)
end) 
else '' end) case_component_part,
(case when substr(li1.prodtyp,1,2) = 'JC' then
(case 
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then pd.description else '' end) is null 
            then (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then pd1.description else '' end)
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then pd.description else '' end) is null and  
        (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then pd1.description else '' end) is null
            then (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then pd2.description else '' end)
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then pd.description else '' end) is null and  
        (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then pd1.description else '' end) is null and 
        (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then pd2.description else '' end) is null
            then (case when substr(pd3.prodtyp,1,2) in ('JC','TR') then pd3.description else '' end)
    else (case when substr(pd.prodtyp,1,2) in ('JC','TR') then pd.description else '' end)
end) 
else '' end) case_description,
(case when substr(li1.prodtyp,1,2) = 'JC' then substr(li1.prodtyp,1,2) else '' end) case_prod,
(case when substr(li2.prodtyp,1,2) = 'TR' then 
(case 
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null 
            then (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end)
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null and  
        (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end) is null
            then (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then ps2.component_part else '' end)
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null and  
        (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end) is null and 
        (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then ps2.component_part else '' end) is null
            then (case when substr(pd3.prodtyp,1,2) in ('JC','TR') then ps3.component_part else '' end)
    else (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end)
end) 
else '' end) tray_component_part,
(case when substr(li2.prodtyp,1,2) = 'TR' then 
(case 
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then pd.description else '' end) is null 
            then (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then pd1.description else '' end)
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then pd.description else '' end) is null and  
        (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then pd1.description else '' end) is null
            then (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then pd2.description else '' end)
    when
        (case when substr(pd.prodtyp,1,2) in ('JC','TR') then pd.description else '' end) is null and  
        (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then pd1.description else '' end) is null and 
        (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then pd2.description else '' end) is null
            then (case when substr(pd3.prodtyp,1,2) in ('JC','TR') then pd3.description else '' end)
    else (case when substr(pd.prodtyp,1,2) in ('JC','TR') then pd.description else '' end)
end) 
else '' end) tray_description,
(case when substr(li2.prodtyp,1,2) = 'TR' then substr(li2.prodtyp,1,2) else '' end)  tray_prod
from leos_item li, oes_customer oc, product_structure ps, part_description pd, 
product_structure ps1, part_description pd1, product_structure ps2, part_description pd2,
product_structure ps3, part_description pd3,
leos_item li1, leos_item li2 
where li.cugrp = 'VDC'
and li.item_type = 'FP' 
and li.cunr = oc.cunr
and li.av_part_no = ps.parent_part (+)
and ps.component_part = pd.part_no (+)
and ps.component_part = ps1.parent_part (+)
and ps1.component_part = pd1.part_no (+)
and ps1.component_part = ps2.parent_part (+)
and ps2.component_part = pd2.part_no (+)
and ps2.component_part = ps3.parent_part (+)
and ps3.component_part = pd3.part_no (+)
and (case 
        when
            (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null 
                then (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end)
        when
            (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null and  
            (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end) is null
                then (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then ps2.component_part else '' end)
        when
            (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null and  
            (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end) is null and 
            (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then ps2.component_part else '' end) is null
                then (case when substr(pd3.prodtyp,1,2) in ('JC','TR') then ps3.component_part else '' end)
        else (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end)
    end) = li1.av_part_no
and (case 
        when
            (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null 
                then (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end)
        when
            (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null and  
            (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end) is null
                then (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then ps2.component_part else '' end)
        when
            (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end) is null and  
            (case when substr(pd1.prodtyp,1,2) in ('JC','TR') then ps1.component_part else '' end) is null and 
            (case when substr(pd2.prodtyp,1,2) in ('JC','TR') then ps2.component_part else '' end) is null
                then (case when substr(pd3.prodtyp,1,2) in ('JC','TR') then ps3.component_part else '' end)
        else (case when substr(pd.prodtyp,1,2) in ('JC','TR') then ps.component_part else '' end)
    end) = li2.av_part_no
and li.catnr = 'GTSTCD100'

谢谢,SMORF

1 个答案:

答案 0 :(得分:0)

1.分离所有差异列

2.使用所有差异列制作子查询,然后转换为数据透视表

3.再次使用主查询加入

SELECT CASE_COMPONENT_PART
     , CASE_DESCRIPTION
     ,CASE_PROD

FROM table_a

LEFT JOIN (

    SELECT id [PK_for_join_first_query]
         ,TRAY_COMPONENT_PART
         ,TRAY_DESCRIPTION,TRAY_PROD

    FROM table_b -- Pivot this Query
)
相关问题