当前位置: 代码迷 >> Oracle管理 >> 一个SQL连接的有关问题
  详细解决方案

一个SQL连接的有关问题

热度:136   发布时间:2016-04-24 04:16:57.0
一个SQL连接的问题

select u.c_measuresiteid,t.c_extendfieldd,count(t.c_measuredocid) n_count,sum(t.n_suttleweight)/1000 n_weight
from TMSR_MEASUREDOCINFO t
left join TB_MEASURESITEQCINFO u
 on t.c_winsvsdatamutual = u.c_measuresiteid
where  t.c_carrytoolstypeid = 'QC'
and t.i_msrfinishflag = '1'
and t.c_winsvsdatamutual is not null
and t.c_measurebegindate >= '2014-07-27 00:00:00'
and t.c_measurebegindate <= '2014-07-28 00:00:00'
group by u.c_measuresiteid ,t.c_extendfieldd
order by u.c_measuresiteid


查询结果是下图所示


TB_MEASURESITEQCINFO 表里有所有的c_measuresiteid,c_extendfieldd的值有4个(1,3,5,7)
我想得到的效果是将没有的统计值都显示为0,如下所示

001    1   88  1676.86
001    3   80   2474.76
001    5    0     0              --计量站点001业务类型编码为5,没有统计信息,则显示为0
001    7   11   190.56
002   1     0     0             --计量站点002业务类型编码为1,没有统计信息,则显示为0
002   3    75   1624.26 
002   5      0      0            --计量站点002业务类型编码为5,没有统计信息,则显示为0
002   7      0      0            --计量站点002业务类型编码为7,没有统计信息,则显示为0

请各位大神指点,能通过连接查询实现吗
------解决方案--------------------
引用:



select t5.c_measuresiteid, t5.c1, nvl(t4.n_count, 0), nvl(t4.n_weight, 0)
  from (select u.c_measuresiteid,
               t.c_extendfieldd,
               count(t.c_measuredocid) n_count,
               sum(t.n_suttleweight) / 1000 n_weight
          from TMSR_MEASUREDOCINFO t
          left join TB_MEASURESITEQCINFO u
            on t.c_winsvsdatamutual = u.c_measuresiteid
         where t.c_carrytoolstypeid = 'QC'
           and t.i_msrfinishflag = '1'
           and t.c_winsvsdatamutual is not null
           and t.c_measurebegindate >= '2014-07-27 00:00:00'
           and t.c_measurebegindate <= '2014-07-28 00:00:00'
         group by u.c_measuresiteid, t.c_extendfieldd) t4,
       (select c_measuresiteid, t2.c1
          from (select distinct u.c_measuresiteid
                  from TMSR_MEASUREDOCINFO t
                  left join TB_MEASURESITEQCINFO u
                    on t.c_winsvsdatamutual = u.c_measuresiteid
                 where t.c_carrytoolstypeid = 'QC'
                   and t.i_msrfinishflag = '1'
                   and t.c_winsvsdatamutual is not null
                   and t.c_measurebegindate >= '2014-07-27 00:00:00'
                   and t.c_measurebegindate <= '2014-07-28 00:00:00') t1,
               (select 1 c1
                  from dual
                union all
                select 3 c1
                  from dual
  相关解决方案