各位大大你们好,注册很久了 但是第一次发贴.
想请教一个问题.
如果有两个表 t1 和 t2 , t1中有列A,t2中有列B.需求是 如果当t1中的A等于t2中的B时那么 显示'已参照'我想到的办法是用case whencase when A=B then '已参照' esle '' end那么在实际使用的时候会出现一个问题,会出现2行数据,一行是已参照,一行是空.
因为A列中的一个值可能等于B中的一列,但是B列中还有其他的值就不等于,所以只要是有等于数据固定会输出一个为空的行.想知道,怎样才能当 B列中有一个等于A列的时候就 只输出一行 已参照,不输出空的.先谢谢大家了.

解决方案 »

  1.   

    没有一个人呀 - - 伤心...付下代码把- -select f.zxmmc 排号项目,f.zphrq 排号日期,f.vcname 客户姓名,f.zkey 排号合同号,
           case when f.zkey=f2.vlastbillcode  then '已参照'end 是否参照
    from   (select zxmmc,zphrq,c.vcname,zkey from
           (select z.xmmc zxmmc,z.phrq zphrq,z.paihaokey zkey,a.pk_customer zpk_customer
            from(select fdc_bd_project.vname xmmc,
                  sm_user.user_name zygw,ps_so_queue.dapprovedate phrq,ps_so_queue.nyqueuemny ysphf,
                  ps_so_queue.pk_queue,
                  ps_so_queue.vqueuebargainnum paihaokey      
             from ps_so_queue_confine
                  left join fdc_bd_project on ps_so_queue_confine.pk_project_build=fdc_bd_project.pk_project       
                  left join ps_so_queue on ps_so_queue_confine.pk_queue=ps_so_queue.pk_queue
                  left join sm_user on ps_so_queue.voperatorid=sm_user.cuserid 
            where ps_so_queue_confine.dr=0 and ps_so_queue.dr=0 ) z 
                  left join PS_SO_QUEUE_CUSTOMER a on z.pk_queue=a.pk_queue) a1
                  left join ps_cmg_customer c on a1.zpk_customer=c.pk_customer)f, --排号
                  (select  vlastbillcode from   
                  (select ps_so_engage.vlastbillcode
                  from ps_so_engage where vlastbill='9466' and  vlastbillcode is not null
                  and ps_so_engage.dr='0')    --预定 
                  union
                  (select ps_so_subsc.vlastbillcode
                  from ps_so_subsc where vlastbill='9466' and  vlastbillcode is not null
                  and ps_so_subsc.dr='0') --认购
                  union
                  (select ps_so_sign.vlastbillcode
                 from ps_so_sign where vlastbill='9466' and  vlastbillcode is not null and ps_so_sign.dr='0'))f2  --签约
    group by f.zxmmc,f.zphrq,f.vcname,f.zkey,case when f.zkey=f2.vlastbillcode then '已参照'end      
    order by f.vcname
      

  2.   


    select case
             when t2.b is not null then
              '已参照'
             else
              'na'
           end ca
      from t1
      left outer join t2
        on t1.a = t2.b
      

  3.   

    要过滤掉非  已参照的数据
    select case
             when t2.b is not null then
              '已参照'
             else
              'na'
           end ca
      from t1
      left outer join t2
        on t1.a = t2.b
     where t2.b is not null
      

  4.   

    但是我还有其他数据啊 他们可能是未参照的. 我不能只输出已参照而必须是想办法 把通过case when 做出来了的一条未参照消除掉