现有个查询语句, 主要关联产品表,由日期检索出产品的生产和销售数量。如下:
产品表  CP (id,name)
生产情况表 SC(num,cp_id,fdate)
销售情况表 XS(num,cp_id,fdate)
i_inputDate 输入的日期。
select sc.num snum,xs.num xnum ,(xs.num/sc.num) bili
from sc,xs,cp 
where sc.cp_id = cp.id and xs.cp_id = cp.id and sc.fdate =i_inputDate  and xs.fdate =i_inputDate。
当生产或销售表中 某个日期上没有记录时,SQL检索的结果为空,该如何修改SQL 使得 即使某个日期上无记录时,也能检索出数据( 无记录的数据为null 就行。)

解决方案 »

  1.   

    select sc.num snum,xs.num xnum ,(xs.num/sc.num) bili
    from sc,xs,cp 
    where sc.cp_id = cp.id and xs.cp_id = cp.id and (sc.fdate =i_inputDate OR sc.fdate is null)  and (xs.fdate =i_inputDate OR xs.fdate is null)
      

  2.   

    select sc.num snum, xs.num xnum, (xs.num / sc.num) bili
      from sc
      left join cp
        on sc.cp_id = cp.id
      left join xs
        on xs.cp_id = cp.id
     where sc.fdate = i_inputDate
       and xs.fdate = i_inputDate
      

  3.   

    不行。
    把各自表中对应的月份数据检索出来。
    例如生产表9月份没数据,而销售表9月份有数据,
    我希望 显示结果为A , 如果都没记录我希望结果为B,不管怎样都不要no_data_found
      生产数量   销售数量  比例
    A  null        999       null
    B  null        null      null
      

  4.   

    不行呀,  where 后条件不满足时 就没结果
      

  5.   

    有记录就抓取出来,没记录也抓取出来(用null)
    现在 where后面 日期比较条件不满足时 检索出来的记录为0,我希望也检索出数据用 null来代替数量
      

  6.   

    不行呀,  where 后条件不满足时 就没结果那你直接把  where sc.fdate = i_inputDate
       and xs.fdate = i_inputDate 条件去掉吧,,看你这需求,没啥用
      

  7.   

    不行呀,  where 后条件不满足时 就没结果那你直接把  where sc.fdate = i_inputDate
       and xs.fdate = i_inputDate 条件去掉吧,,看你这需求,没啥用
    这条件去掉, 那不把所有日期的数据都查出来?
      

  8.   

    with tt as (select to_char(add_months(sysdate,0-level),'yyyymm') as months from dual connect by level <36)
    select b.num snum,c.num xnum,decode(b.num,null,'null',0,'null',c.num/b.num) bl from tt t,sc b, xs c
    where t.months=to_char(b.fdate,'yyyymm')(+) and t.months=to_char(c.fdate,'yyyymm')(+)
      

  9.   

    不行呀,  where 后条件不满足时 就没结果那你直接把  where sc.fdate = i_inputDate
       and xs.fdate = i_inputDate 条件去掉吧,,看你这需求,没啥用
    这条件去掉, 那不把所有日期的数据都查出来?
    select sc.num snum, xs.num xnum, (xs.num / sc.num) bili
      from sc
      left join cp
        on sc.cp_id = cp.id
      left join xs
        on xs.cp_id = cp.id
        and xs.fdate = i_inputDate
      where sc.fdate = i_inputDate
    这样呢