日期:2014-05-17 浏览次数:20546 次
SELECT id ,
name ,
MIN(date) date
FROM ( SELECT id ,
name ,
date
FROM tb1
UNION ALL
SELECT id ,
name ,
date
FROM tb2
) a
GROUP BY id ,
name
if object_id('table1') is not null
drop table table1
create table table1
(
id int,
name nvarchar(20),
[date] date
)
go
insert into table1
select 1,'1号','2001-10-21' union all
select 2,'2号','2001-10-20'
if object_id('table2') is not null
drop table table2
create table table2
(
id int,
name nvarchar(20),
[date] date
)
go
insert into table2
select 1,'1号','2001-10-22' union all
select 3,'3号','2001-10-22'
select isnull(table1.id,table2.id) id,
isnull(table1.name,table2.name) name,
case when isnull(table1.date,'1900-01-01')>isnull(table2.date,'1900-01-01') then table1.date else table2.date end [Date]
from table1
full join table2 on table1.id =table2.id
--结果
--1 1号 2001-10-22
--2 2号 2001-10-20
--3 2号 2001-10-22