两表连接合并

select a.pdate,sum(sumcost) as sumcost,sum(sumincome) as sumincome from (
select pdate,0 as sumcost,sumincome from B
union all select pdate,sumcost,0 as sumincome from A
) as a group by a.pdate order by a.pdate

 

USE [test] GO

/****** Object:  Table [dbo].[A]    Script Date: 08/20/2014 09:34:36 ******/ SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER ON GO

CREATE TABLE [dbo].[A](  [pdate] [nvarchar](max) NULL,  [sumcost] [nvarchar](max) NULL ) ON [PRIMARY]

GO

 

USE [test] GO

/****** Object:  Table [dbo].[B]    Script Date: 08/20/2014 09:34:57 ******/ SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER ON GO

CREATE TABLE [dbo].[B](  [pdate] [nvarchar](max) NULL,  [sumincome] [nvarchar](max) NULL ) ON [PRIMARY]

GO

 

posted @ 2014-08-20 09:36  jlf19881031  阅读(168)  评论(0编辑  收藏  举报