A表是客户信息表(用户名不重复),字段: userName、UserLl
B表是业务表(各用户都有很多业务),字段:UserName 、YwMoney
现在想将A表和B表综合插入到C表中
C表字段: UserName UserLl YwMoney(要合计)
请问这个SQL要怎么写呢?请高手指点小弟。
------解决方案--------------------
- SQL code
insert c(UserName,UserLl,YwMoney)select a.userName,a.UserLl,isnull(b.YwMoney,0)from aleft join (select UserName,sum(YwMoney) as YwMoney from b group by UserName) bon a.UserName=b.UserName
------解决方案--------------------
------解决方案--------------------
- SQL code
select t1.userName,t1.UserLl,YwMoney=sum(YwMoney) into C from A t1 inner join B t2 on t1.userNmae=t2.userName group by t1.userName,t1.UserLl
------解决方案--------------------
- SQL code
insert into cselect *from ( select userName,UserLl,0 as YwMoneyfrom a union all select UserName,'',sum(YwMoney) as YwMoney from b group by UserName)t
------解决方案--------------------