table1
项目   数量1                        
A      1
A      2
B      3
C      2
C      3
table2
项目   数量2                       
A      2
A      3
A      3
C      1
C      1
求汇总
项目   数量1   数量2 
A      3       8
B      3       0
C      5       2  

解决方案 »

  1.   

    select table1.项目,sum(table1.数量1),sum(table2.数量2) 
    from table1,table2
    where table1.项目 = table2.项目
    group by table1.项目
      

  2.   

    select t1.c1, sum(t1.c2) as count1, sum(t2.c2) as count2 from t1 right join t2 on t1.c1 = t2.c1 group by t1.c1
      

  3.   

    嗯,无兄的考虑不周全,可能A里有的项目,B里没有
    应该是把所有的项目筛选出来
    Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    然后用子查询
    select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
    数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from 
    (Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    )
    b
      

  4.   

    table2里有的项目 table1里可能没有
      

  5.   

    用一条SQl能解决吗,要效率。
      

  6.   

    嗯,无兄的考虑不周全,可能A里有的项目,B里没有
    应该是把所有的项目筛选出来
    Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    然后用子查询
    select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
    数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from 
    (Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    )
    b
    嗯,无兄的考虑不周全,可能A里有的项目,B里没有
    应该是把所有的项目筛选出来
    Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    然后用子查询
    select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
    数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from 
    (Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    )
    b
    嗯,无兄的考虑不周全,可能A里有的项目,B里没有
    应该是把所有的项目筛选出来
    Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    然后用子查询
    select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
    数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from 
    (Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    )
    b
    嗯,无兄的考虑不周全,可能A里有的项目,B里没有
    应该是把所有的项目筛选出来
    Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    然后用子查询
    select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
    数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from 
    (Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    )
    b
    嗯,无兄的考虑不周全,可能A里有的项目,B里没有
    应该是把所有的项目筛选出来
    Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    然后用子查询
    select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
    数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from 
    (Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    )
    b
    嗯,无兄的考虑不周全,可能A里有的项目,B里没有
    应该是把所有的项目筛选出来
    Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    然后用子查询
    select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
    数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from 
    (Select Distinct 项目 from (
    Select table1.项目 as 项目 from table1 
    union all
    Select table2.项目 from table2)
    a
    )
    b