两个数据库数据对比,求救高手

zhangbing530 2013-11-06 02:41:45
两个年度的数据库中,都存有项目的数据,我要做一张报表 出来 对比两个年度每个项目的数据情况 以及增减情况。两个年度的项目都要列出来对比,如遇到只有其中一个年度才有的项目 也要列出来,没有这个项目的年度数据就显示为0 具体如下表

表1 表2
项目名称 数据 项目名称 数据
A 10 A 20
B 20 C 30
C 30 E 50
D 40


结果 :项目名称 数据1 数据2
A 10 20
B 20 0
C 30 30
D 10 0
E 0 50
...全文
407 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ny-6000 2013-11-18
  • 打赏
  • 举报
回复
full JOIN 有这个,别的自己想着写,就OK了.
Blessed_Chuan 2013-11-18
  • 打赏
  • 举报
回复
引用 8 楼 DBAXMMDS 的回复:

---此段代码是在sql server 2012上创建并运行的
--创建表1
create table a
(item_name char(1),
data1 tinyint
)
--向表1插入数据
insert into a
values( 'A',10),  
( 'B',20), 
( 'C',30),  
( 'D',40);
--创建表2
create table b
(item_name char(1),
data2 tinyint
)
--向表2插入数据
insert into b
values( 'A',20),  
( 'C',30),  
( 'E',50);
--解决语句
;with aAndB as 
(
select item_name,data1 as data,1 as item from a
union all
select item_name,data2 as data,2 as item from b
)
select item_name,  max(case item when 1 then data else 0 end )as item_A,max(case item when 2 then data else 0 end) as item_B
from aAndB 
group by item_name

--结果展示
/*

item_name item_A      item_B
--------- ----------- -----------
A         10          20
B         20          0
C         30          30
D         40          0
E         0           50

(5 行受影响)

*/
—+1
极品老土豆 2013-11-16
  • 打赏
  • 举报
回复

---此段代码是在sql server 2012上创建并运行的
--创建表1
create table a
(item_name char(1),
data1 tinyint
)
--向表1插入数据
insert into a
values( 'A',10),  
( 'B',20), 
( 'C',30),  
( 'D',40);
--创建表2
create table b
(item_name char(1),
data2 tinyint
)
--向表2插入数据
insert into b
values( 'A',20),  
( 'C',30),  
( 'E',50);
--解决语句
;with aAndB as 
(
select item_name,data1 as data,1 as item from a
union all
select item_name,data2 as data,2 as item from b
)
select item_name,  max(case item when 1 then data else 0 end )as item_A,max(case item when 2 then data else 0 end) as item_B
from aAndB 
group by item_name

--结果展示
/*

item_name item_A      item_B
--------- ----------- -----------
A         10          20
B         20          0
C         30          30
D         40          0
E         0           50

(5 行受影响)

*/
treemo 2013-11-16
  • 打赏
  • 举报
回复
这个问题我也遇到过 已经解决了
Yole 2013-11-08
  • 打赏
  • 举报
回复
全连接~然后对结果进行处理一下!
treemo 2013-11-08
  • 打赏
  • 举报
回复
这个问题我也遇到过 你试试这个 自己改下表明字段 select isnull(t1.send_date,t2.send_date) send_date,isnull(t1.team,0) team,isnull(t2.person,0) person from ( select send_date,sum(marathonman)+sum(marathonwomen)+sum(halfmarathon)+sum(tenmarathon)+sum(filemarathon) team from T_xmim_Group_Provide_log group by send_date ) t1 FULL OUTER join ( select send_date,sum(1) person from T_xmim_provide_log group by send_date ) t2 on t1.send_date=t2.send_date order by send_date desc 应该是你想要的
LongRui888 2013-11-06
  • 打赏
  • 举报
回复
这样就可以:



if object_id('tb1') is not null drop table tb1
go

create table tb1 (项目名称 nvarchar(2),数据 int)
insert into tb1
select 'A',10 union all
select 'B',20 union all
select 'C',30 union all
select 'D',40

if object_id('tb2') is not null drop table tb2
go
create table tb2 (项目名称 nvarchar(2),数据 int)
insert into tb2
select 'A',20 union all
select 'C',30 union all
select 'E',50


;with t
as
(
select 项目名称
from tb1
union 
select 项目名称
from tb2
)

select t.项目名称,
       coalesce(t1.数据,0) as 数据1 ,
       coalesce(t2.数据,0) as 数据2 
from t
left join tb1 t1 on t.项目名称 =t1.项目名称
left join tb2 t2 on t.项目名称 =t2.项目名称
/*
项目名称	数据1	数据2
A	10	20
B	20	0
C	30	30
D	40	0
E	0	50
*/
小魚人 2013-11-06
  • 打赏
  • 举报
回复
全連接?
Mr_Nice 2013-11-06
  • 打赏
  • 举报
回复
if object_id('[表1]') is not null drop table [表1]
go
create table [表1] (项目名称 nvarchar(2),数据 int)
insert into [表1]
select 'A',10 union all
select 'B',20 union all
select 'C',30 union all
select 'D',40

if object_id('[表2]') is not null drop table [表2]
go
create table [表2] (项目名称 nvarchar(2),数据 int)
insert into [表2]
select 'A',20 union all
select 'C',30 union all
select 'E',50

select * from [表1]
select * from [表2]


select s.项目名称,isnull(a.数据,0) as 数据1 ,isnull(b.数据,0) as 数据2 from (
select 项目名称
from [表1]
union 
select 项目名称
from [表2]) S
left join [表1] A on s.项目名称 =A.项目名称
left join [表2] B on s.项目名称 =B.项目名称

/*
A	10	20
B	20	0
C	30	30
D	40	0
E	0	50*/
發糞塗牆 2013-11-06
  • 打赏
  • 举报
回复
--> 测试数据:[表1]
if object_id('[表1]') is not null drop table [表1]
go 
create table [表1]([项目名称] varchar(1),[数据] int)
insert [表1]
select 'A',10 union all
select 'B',20 union all
select 'C',30 union all
select 'D',40
--------------开始查询--------------------------


--> 测试数据:[表2]
if object_id('[表2]') is not null drop table [表2]
go 
create table [表2]([项目名称] varchar(1),[数据] int)
insert [表2]
select 'A',20 union all
select 'C',30 union all
select 'E',50


select ISNULL(a.[项目名称],b.[项目名称]) AS [项目名称],ISNULL(a.数据,0)[数据1],ISNULL(b.数据 ,0)[数据2]
from [表1] a full JOIN [表2] b ON a.[项目名称]=b.[项目名称]
----------------结果----------------------------
/* 
项目名称 数据1         数据2
---- ----------- -----------
A    10          20
B    20          0
C    30          30
D    40          0
E    0           50
*/

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧