现在有四张表,
表1:
custdo_id(顾客制作单号) cust_name(顾客姓名) cust_size(冲印项目) xh(项目序号) num(冲印数量)
表2:
custdo_id(顾客制作单号) cust_name(顾客姓名) cust_size(冲印项目) xh(项目序号) h_num(回单数量)
表3:
custdo_id(顾客制作单号) cust_name(顾客姓名) cust_size(冲印项目) xh(项目序号) f_num(返工数量)
表4:
custdo_id(顾客制作单号) cust_name(顾客姓名) cust_size(冲印项目) xh(项目序号) zk_num(重开数量)
现在的问题是,如果要查询表1中某一个顾客中具体一个冲印项目的同时,要查询该顾客冲印项目的回单数量,返工数量,重开数量,如数量为空,也显示为空??急~~~~~

解决方案 »

  1.   

    select custdo_id,cust_name,cust_size,xh,sum(num) as num,sum(h_num) as h_num,sum(f_num) as f_num,sum(zk_num) as zk_num
    from (
    select custdo_id,cust_name,cust_size,xh,num,cast(null as int) as h_num,cast(null as int) as f_num,cast(null as int) as zk_num
    from table1
    where 查询条件
    union all
    select custdo_id,cust_name,cust_size,xh,cast(null as int) as num,h_num,cast(null as int) as f_num,cast(null as int) as zk_num
    from table2
    where 查询条件
    union all
    select custdo_id,cust_name,cust_size,xh,cast(null as int) as num,cast(null as int) as h_num,f_num,cast(null as int) as zk_num
    from table3
    where 查询条件
    union all
    select custdo_id,cust_name,cust_size,xh,cast(null as int) as num,cast(null as int) as h_num,cast(null as int) as f_num,zk_num
    from table4
    where 查询条件
    ) as t
    group by custdo_id,cust_name,cust_size,xh
      

  2.   

    四个表是否通过custdo_id关联?select 表1.*,isnull(h_num,0) as h_num,isnull(f_num,0) as f_num,isnull(zk_num,0) as zk_num
    from 表1 left join 表2 on 表1.custdo_id = 表2.custdo_id
             left join 表3 on 表1.custdo_id = 表3.custdo_id
             left join 表4 on 表1.custdo_id = 表4.custdo_id
      

  3.   

    select b1.*,isnull(b2.h_num,''),isnull(b3.f_num,''),isnull(b4.zk_num,'')  
    from b1,b2,b3,b4
    where 
    b1.custdo_id=b2.custdo_id and b2.custdo_id=b3.custdo_id and custdo_id=b4.custdo_id and b1.cust_size=b2.cust_size and b2.cust_size=b3.cust_size  and b3.cust_size=b4.cust_size 
      

  4.   

    select cust_size,isnull(h_num,0) as h_num,isnull(f_num,0) as f_num,isnull(zk_num,0) as zk_num
    from 表1 
    left join 表2 on 表1.custdo_id = 表2.custdo_id
    left join 表3 on 表1.custdo_id = 表3.custdo_id
    left join 表4 on 表1.custdo_id = 表4.custdo_id
      

  5.   

    用楼上连接方式,把cust_name(顾客姓名) cust_size(冲印项目)作为group by 就行了
      

  6.   

    说测试结果的时候请说明测试谁的哪个语句如果用连接,按照楼主的要求,上面的朋友都应该把isnull去掉
      

  7.   

    估计连接条件至少两个字段,说不准4各字段select 表1.*,h_num,f_num,zk_num
    from 表1 left join 表2 on 表1.custdo_id = 表2.custdo_id and 表1.cust_size = 表2.cust_size and 表1.xh = 表2.xh
             left join 表3 on 表1.custdo_id = 表3.custdo_id and 表1.cust_size = 表3.cust_size and 表1.xh = 表3.xh
             left join 表4 on 表1.custdo_id = 表4.custdo_id and 表1.cust_size = 表4.cust_size and 表1.xh = 表4.xh
      

  8.   

    ziyulin0311(echo) ( ) 信誉:100    Blog  2006-10-19 09:56:00  得分: 0  
     
       如何把重复的冲印尺寸去掉??
      
     ======================================从你上门贴出的数据来看,有两种可能
    1.你的某个表的数据重复
    2.连接条件不够我觉得这几个表的关系楼主应该先弄明白吧,到底这几个表是根据那几个字段关联的?