这是我的写法,但是感觉不太简洁 select a.xx, a.y, b.y 
from  
(select table1.xx, sum(table2.y) y 
from table1 left join table2 on table1.x=table2.x 
group by table1.xx) as a , (select table1.xx, sum(table3.y) y 
from table1 left join table3 on table1.x=table3.x 
group by table1.xx) as b where a.xx=b.xx and (a.y<>0 or b.y<>0) 

解决方案 »

  1.   

    select aa.xx,
           sum(isnull(aa.by,0)) as by,
           sum(isnull(aa.cy,0)) as cy
    from (select a.xx,b.y as by,c.y as cy 
          from t1 a left join t2 b on a.x=b.x
                       left join t3 c on a.x=c.x ) aa
    group by aa.xx
      

  2.   

    此方法不能过滤掉xx4,如果table1中xx4、xx5、xx6、xx7、xx8 这样的数据很多的话,将非常麻烦
      

  3.   

    select aa.xx,
           sum(isnull(aa.by,0)) as by,
           sum(isnull(aa.cy,0)) as cy
    from (select a.xx,b.y as by,c.y as cy 
          from t1 a left join t2 b on a.x=b.x
                       left join t3 c on a.x=c.x
          where b.y is not null and c.y is not null   --加条件试试 
         ) aa
    group by aa.xx
      

  4.   

    如果table1中xx4、xx5、xx6、xx7、xx8 这样的数据很多的话,将非常麻烦很多的时候怎么处理,是合成一条,还是单条显示出来????
      

  5.   

    谢谢小李兄给的回复。我的意思是不要xx4、xx5、xx6、xx7等数据,只显示table2与table3的内容!
      

  6.   

    你看这样如何(通过你的启发):
    select a.xx,sum(isnull(aa.by,0)) as by,sum(isnull(aa.cy,0)) as cy 
    from  table1 a left join table2 b on a.x=b.x
                       left join table3 c on a.x=c.x
    where b.y is not null or c.y is not null      //这里是or而不是and    
    group by a.xx
      

  7.   

    try:select a.xx,isunll(b.y,0) as y1,isnull(c.y,0) as y2 from table1 a,(select a,sum(y) as y from table2 group by a) b,(select a,sum(y) as y from table3 group by a) cwhere a.x=b.x and a.x=c.x
      

  8.   

    select c.xx,sum(a.y) as a.y,sum(b.y) as b.y from table2 a left join table3 b on a.x=b.x left join table1 c on a.x=c.x 
    group by c.xx
      

  9.   

    select c.xx,sum(a.y) as a.y,sum(b.y) as b.y from table2 a left join table3 b on a.x=b.x left join table1 c on a.x=c.x 
    group by c.x
      

  10.   

    select c.xx,sum(isunll(a.y,0)) as a.y,sum(isunll(b.y,0)) as b.y from table2 a left join table3 b on a.x=b.x left join table1 c on a.x=c.x 
    group by c.x