a表
item_id pay bz
1001 5 吃饭
1002 10 坐车
1001 2 买书
1002 1 吃饭
1003 10 坐车b表
item_id total
1001 100
1002 50
1003 50把a、b表中的数据合并成c表c表
item_id sum_pay total
1001 7 100
1002 11 50
1003 10 50
也就是把a表中的相同item_id 的pay数据相加后,再与b表中对应的item_id组成新的表c
请大虾帮忙!
item_id pay bz
1001 5 吃饭
1002 10 坐车
1001 2 买书
1002 1 吃饭
1003 10 坐车b表
item_id total
1001 100
1002 50
1003 50把a、b表中的数据合并成c表c表
item_id sum_pay total
1001 7 100
1002 11 50
1003 10 50
也就是把a表中的相同item_id 的pay数据相加后,再与b表中对应的item_id组成新的表c
请大虾帮忙!
From B表 Group by item_id
Update C表 Set total = S.total
From A表 S, C表 A Where S.item_id = A.item_id
Insert C表 (item_id, sum_pay) Select item_id, sum(sum_pay)
From A表 Group by item_id
Update C表 Set total = S.total
From B表 S, C表 A Where S.item_id = A.item_id
left join (select item_id,sum(pay) pay from a表 group by item_id) V1
on b.item_id=V1.item_id
into c表
item_id pay bz
1001 5 吃饭
1002 10 坐车
1001 2 买书
1002 1 吃饭
1003 10 坐车
b表
item_id total
1001 100
1001 20
1002 50
1002 50
1003 50
把a、b表中的数据合并成c表
c表
item_id sum_pay total
1001 7 120
1002 11 100
1003 10 50
也就是把a表中的相同item_id 的pay数据相加,b表中的相同item_id 的total数据相加,最后把a与b表中对应的item_id组成新的表c
请大虾帮忙!
第一次写错了,本帖我已经加分,请DragonBill(月满西星)和sankis() 再帮忙,谢谢!
From A表 Group by item_idUpdate C表 Set total =sum( S.total)
From B表 S, C表 C Where S.item_id = C.item_id
gruop by s.Item_id
(select item_id,sum(pay) as sum_pay from a group by item_id) a
inner join
(select item_id,sum(total) as total from b group by item_id) b
on a.item_id=b.item_id==============================
以上在SQLServer2000通过!
高手就是高手!佩服!!!!!!!!!!!!