请教高手关于两表复杂查询统计更新字段问题
有2个表,分别是:table1和table2 
 table1里面有字段ID、AA、BB、CC、DD、EE、FF、GG、SUM、LSNUM 
 table2里面有字段ID、AA、BB、CC、DD、EE、FF、GG 
 ID字段是自增加1,其他字段全部是数字型。 
 现在要做的就是将table2里面的AA、BB、CC、DD、EE、FF、GG和table1里面的AA、BB、CC、DD、EE、FF、GG相比,如果两个表的AA、BB、CC、DD、EE、FF、GG这几个字段的值完成相同的话,则将table1里面的SUM进行+1; 
 如果不同的话,则再进行判断,table2里面的AA、BB、CC、DD、EE、FF、GG字段里面的数字在table1里面AA、BB、CC、DD、EE、FF、GG里面出现的次数,并将出现的次数统计到LSNUM里面。如下:   
 table1表: 
 ID      AA            BB            CC               DD                  EE                  FF                     GG               SUM            LSNUM 
 1         100         600         1000         2000            3000         4000               8000            0                           0 
 2         300         500         600            1000            4000         6000               8000            0                           0 
 table2 
 ID      AA            BB            CC               DD               EE                  FF               GG 
 1         100         600         1000         2000         3000         4000         8000   
 希望通过SQL查询更新后,table1表中的SUM、LSNUM值将变化为以下: 
 table1表: 
 ID      AA            BB            CC               DD                  EE                  FF                     GG               SUM            LSNUM 
 1         100         600         1000         2000            3000         4000               8000            1                           0 
 2         300         500         600            1000            4000         6000               8000            0                           4 
 第一条记录完全相同,所以SUM的值+1 
 第二条记录有4个数字相同,则LSNUM=4     
------解决方案--------------------select  ', '+cast(aa as varchar)+ ', '+cast(bb as varchar)+ ', '+cast(cc as varchar)+ ', '+cast(dd as varchar)+ ', '+cast(ee as varchar)+ ', '+cast(ff as varchar)+ ', '+cast(gg as varchar)+ ', ' as indexchar 
 into #tmp from table1   
 update t1 set [sum]=t2.total 
 from table1 t1 inner join  
 (select aa,bb,cc,dd,ee,ff,gg,count(1) total from table2 group by aa,bb,cc,dd,ee,ff,gg) t2 
 on t1.aa=t2.aa and t1.bb=t2.bb and t1.cc=t2.cc and t1.dd=t2.dd and t1.ee=t2.ee and t1.ff=t2.ff and t1.gg=t2.gg   
 update t2 set lsum=a+b+c+e+f+g 
 from t2  inner join  
 ( 
 	select t2.aa,t2.bb,t2.cc,t2.dd,t2.ee,t2.ff,t2.gg case wh