日期:2014-05-18  浏览次数:20612 次

着急,请大神帮忙~急~~~~~~~~~~~~~~~~~~~~~~
现在有两张表 
A表 
ksdm zje
01 100
02 200
03 250

B表
ksdm zje
02 150
03 100
04 500

怎么得到表
ksdm zje 
01 100
02 350
03 350
04 500


------解决方案--------------------
SQL code
----------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2012-07-11 10:55:48
-- Version:
--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) 
--    Apr 22 2011 11:57:00 
--    Copyright (c) Microsoft Corporation
--    Enterprise Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go 
create table [A]([ksdm] varchar(2),[zje] int)
insert [A]
select '01',100 union all
select '02',200 union all
select '03',250
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go 
create table [b]([ksdm] varchar(2),[zje] int)
insert [b]
select '02',150 union all
select '03',100 union all
select '04',500
--------------开始查询--------------------------
select
 ksdm,sum(zje) as zje 
from
 (select * from A union all select * from b)t
group by
  ksdm
----------------结果----------------------------
/* ksdm zje
---- -----------
01   100
02   350
03   350
04   500

(4 行受影响)


*/