我现在经过一系列查询得到以下结果:name   风险等级  数量
---------------------
a       高        2
a       中        5
b       高        2
c       低        8但是我想得到的是这样的:name   风险等级  数量
---------------------
a       高        2
a       中        5
a       低        0
b       高        2
b       中        0
b       低        0
c       低        8
c       高        0
c       中        0也就是说我想让各个风险等级的数量是0的也显示出来,请问我如何写这个sql?想了半天没有结果,期望大家能给我点思路,谢谢!

解决方案 »

  1.   


    --原结果集跟一个高中低的临时表,进行full join 
    --你试试,我没测试with tab1 as(
    select 'a' name, '高' lv, 2 qty from dual union all 
    select 'a', '中', 5 from dual union all 
    select 'b', '高', 2 from dual union all 
    select 'c', '低', 8 from dual
    ),
    tab2 as(
    select '高' lv from dual union all
    select '中' from dual union all 
    select '低' from dual
    )
    select a.name,b.lv,nvl(a.qty,0) 
    from tab1 a full join tab2 b
    on a.lv=b.lv
      

  2.   

    楼上说的对的,最重要的是用 full join,全外连接实现。
    我提一个意见,因为不知道你的表结构,你的表对于数量必须是not null,不然全外连接时看到右边的会是空值。
      

  3.   

    补充一下
    with tab1 as(
      select 'a' name, '高' lv, 2 qty from dual union all 
      select 'a', '中', 5 from dual union all 
      select 'b', '高', 2 from dual union all 
      select 'c', '低', 8 from dual
      ),
    tab2 as(
      select '高' lv from dual union all
      select '中' from dual union all 
      select '低' from dual
      )
    SELECT b.name, b.lv, nvl(a.qty, 0)
      FROM tab1 a
      FULL JOIN (SELECT DISTINCT NAME, tab2.lv FROM tab1, tab2) b
        ON a.lv = b.lv
       AND a.name = b.name
     ORDER BY b.name
      

  4.   

    with tab1 as(
      select 'a' name, '高' lv, 2 qty from dual union all 
      select 'a', '中', 5 from dual union all 
      select 'b', '高', 2 from dual union all 
      select 'c', '低', 8 from dual
      ),
    tab2 as(
      select '高' lv from dual union all
      select '中' from dual union all 
      select '低' from dual
      )
    SELECT b.name, b.lv, nvl(a.qty, 0)
      FROM tab1 a
      FULL JOIN (SELECT DISTINCT NAME, tab2.lv FROM tab1, tab2) b
        ON a.lv = b.lv
       AND a.name = b.name
     ORDER BY b.name
      

  5.   

    ---的构造数据来左连接
    with tb as
    (select 'a' name, '高' 风险等级 ,2 数量 from dual union all
    select 'a', '中', 5 from dual union all
    select 'b', '高', 2 from dual union all
    select 'c', '低', 8 from dual )
    select a.name,a.风险等级,nvl(d.数量,0) 数量
    from (select name,风险等级
    from (select distinct name from tb) b,
    (select distinct 风险等级 from tb) c) a left join tb d on a.name=d.name and a.风险等级=d.风险等级
    order by a.name
    SQL> with tb as
      2  (select 'a' name, '高' 风险等级 ,2 数量 from dual union all
      3  select 'a', '中', 5 from dual union all
      4  select 'b', '高', 2 from dual union all
      5  select 'c', '低', 8 from dual )
      6  select a.name,a.风险等级,nvl(d.数量,0) 数量
      7  from (select name,风险等级
      8  from (select distinct name from tb) b,
      9  (select distinct 风险等级 from tb) c) a left join tb d on a.name=d.name and a.风险等级=d.风险等级
     10  order by a.name
     11  /
     
    NAME 风险等级          数量
    ---- -------- ----------
    a    低                0
    a    中                5
    a    高                2
    b    高                2
    b    低                0
    b    中                0
    c    低                8
    c    中                0
    c    高                0
     
    9 rows selected
      

  6.   

    我觉的,如果是得到这组数据是为了构造应用中的对象的话,最后不要只是通过SQL来完成,建议通过SQL取出原始数据,在构造应用中的对象时,用代码完成此效果,从要求上看,就是补全'高'、'中'、'低',没有时,都是用0。如果单纯依靠SQL,SQL的执行效率在大数据量时会很低。