【问题标题】:Oracle SQL: return results on one rowOracle SQL:在一行上返回结果
【发布时间】:2015-06-09 08:00:30
【问题描述】:

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

目前,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

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    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
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      相关资源
      最近更新 更多