日期:2014-05-18 浏览次数:20508 次
select isnull(a.名称,b.名称) as 名称,isnull(a.合计,0)+isnull(b.合计,0) as 合计 from A a full join B b on a.名称 = b.名称
------解决方案--------------------
IF OBJECT_ID('tempdb..#A') IS NOT NULL DROP TABLE #A GO IF OBJECT_ID('tempdb..#B') IS NOT NULL DROP TABLE #B GO CREATE TABLE #A (ID INT ,SZ INT) INSERT #A SELECT 1,10 UNION ALL SELECT 2,20 UNION ALL SELECT 3,30 UNION ALL SELECT 4,40 CREATE TABLE #B (SID INT ,SSZ INT) INSERT #B SELECT 3,1 UNION ALL SELECT 4,1 UNION ALL SELECT 6,60 UNION ALL SELECT 7,70 ;WITH AA AS( SELECT * FROM #A A FULL JOIN #B B ON A.ID = B.SID) SELECT ISNULL(ID,SID),ISNULL(SZ,0)+ISNULL(SSZ,0) FROM AA /* 1 10 2 20 3 31 4 41 6 60 7 70 */
------解决方案--------------------
--表A 表B --名称 合计 名称 合计 --1 10 3 1 --2 20 4 1 --3 30 6 60 --4 40 7 70 declare @表A table (