繁体   English   中英

Oracle SQL:在一行上返回结果

[英]Oracle SQL: return results on one row

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

当前,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.分隔所有差异列

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
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM