表employee
EmpID
1
2
3
4
5表bc
empid adate a b c d
1 2011-04-08 1 1 0 0
1 2011-04-09 1 0 0 0
2 2011-04-08 0 1 0 0
5 2011-10-01 1 1 1 1 表dcempid adate e f g h
1 2011-04-08 0 0 2 0
2 2011-04-08 0 0 1 0
3 2011-04-08 0 0 1 0
4 2011-04-09 0 1 0 0
按empid 和adate 生成下表empid adate a b c d e f g h
1 2011-04-08 1 1 0 0 0 0 2 0
1 2011-04-09 1 0 0 0 0 0 0 0
2 2011-04-08 0 1 0 0 0 0 1 0
3 2011-04-08 0 0 0 0 0 0 1 0
4 2011-04-09 0 0 0 0 0 1 0 0
EmpID
1
2
3
4
5表bc
empid adate a b c d
1 2011-04-08 1 1 0 0
1 2011-04-09 1 0 0 0
2 2011-04-08 0 1 0 0
5 2011-10-01 1 1 1 1 表dcempid adate e f g h
1 2011-04-08 0 0 2 0
2 2011-04-08 0 0 1 0
3 2011-04-08 0 0 1 0
4 2011-04-09 0 1 0 0
按empid 和adate 生成下表empid adate a b c d e f g h
1 2011-04-08 1 1 0 0 0 0 2 0
1 2011-04-09 1 0 0 0 0 0 0 0
2 2011-04-08 0 1 0 0 0 0 1 0
3 2011-04-08 0 0 0 0 0 0 1 0
4 2011-04-09 0 0 0 0 0 1 0 0
isnull(a.adate,b.adate) as adate,
isnull(a.a,0) a, isnull(a.b,0) b,isnull(a.c,0) c,isnull(a.d,0)d,
isnull(b.e,0) e,isnull(b.f,0)f,isnull(b.g,0)g,isnull(b.h,0)h
from bc a full join dc b on a.empid=b.empid and a.adate=b.adate
select a.empid,a.adate,isnull(b.a,0)a,isnull(b.b,0)b,isnull(b.c,0)c,isnull(b.d,0)d,a.e,a.f,a.g,a.h
from dc a left join bc b on a.empid = b.empid and a.adate = b.adate
select a.empid,a.adate,
isnull(b.a,0)a,isnull(b.b,0)b,
isnull(b.c,0)c,isnull(b.d,0)d,
a.e,a.f,a.g,a.h
from dc a left join bc b on a.empid = b.empid and a.adate = b.adate
/*按楼主结果是 left join 如果 empid 是5的那条也显示那就改为 full join*/--full join select a.empid,a.adate, --也有可能是 isnull(a.empid,b.empid) isnull(a.adate,b.adate)
isnull(b.a,0)a,isnull(b.b,0)b,
isnull(b.c,0)c,isnull(b.d,0)d,
a.e,a.f,a.g,a.h
from dc a left join bc b on a.empid = b.empid and a.adate = b.adate
select isnull(a.empid,b.empid)empid,
isnull(a.adate,b.adate)adate,
isnull(b.a,0)a,isnull(b.b,0)b,
isnull(b.c,0)c,isnull(b.d,0)d,
isnull(a.e,0)e,isnull(a.f,0)f,
isnull(a.g,0)g,isnull(a.h,0)h
from dc a full join bc b on a.empid = b.empid and a.adate = b.adate
insert into bc
select '1', '2011-04-08', 1, 1 ,0 ,0 union
select '1', '2011-04-09', 1, 0 ,0 ,0 union
select '2', '2011-04-08', 0, 1 ,0 ,0 union
select '5', '2011-10-01', 1, 1 ,1 ,1create table dc(empid char(2),adate varchar(10), e int ,f int, g int, h int)
insert into dc
select '1', '2011-04-08', 0, 1 ,2 ,0 union
select '1', '2011-04-08', 0, 0 ,1 ,0 union
select '2', '2011-04-08', 0, 0 ,1 ,0 union
select '4', '2011-04-09', 0, 1 ,0 ,0
select isnull(a.empid,b.empid) as empid,
isnull(a.adate,b.adate) as adate,
isnull(a.a,0) a, isnull(a.b,0) b,isnull(a.c,0) c,isnull(a.d,0)d,
isnull(b.e,0) e,isnull(b.f,0)f,isnull(b.g,0)g,isnull(b.h,0)h
from bc a full join dc b on a.empid=b.empid and a.adate=b.adate/*
empid adate a b c d e f g h
----- ---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1 2011-04-08 1 1 0 0 0 0 1 0
1 2011-04-08 1 1 0 0 0 1 2 0
1 2011-04-09 1 0 0 0 0 0 0 0
2 2011-04-08 0 1 0 0 0 0 1 0
5 2011-10-01 1 1 1 1 0 0 0 0
4 2011-04-09 0 0 0 0 0 1 0 0
use tempdb;
/*
create table employee
(
EmpID int not null
);
insert into employee(EmpID)
values
(1),(2),(3),(4),(5);create table bc
(
empid int not null,
adate date not null,
a int not null,
b int not null,
c int not null,
d int not null
);
insert into bc(empid,adate,a,b,c,d)
values
(1,'2011-04-08',1,1,0,0),
(1,'2011-04-09',1,0,0,0),
(2,'2011-04-08',0,1,0,0),
(5,'2011-10-01',1,1,1,1);create table dc
(
empid int not null,
adate date not null,
e int not null,
f int not null,
g int not null,
h int not null
);
insert into dc(empid,adate,e,f,g,h)
values
(1,'2011-04-08',0,0,2,0),
(2,'2011-04-08',0,0,1,0),
(3,'2011-04-08',0,0,1,0),
(4,'2011-04-09',0,1,0,0);
按empid 和adate 生成下表empid adate a b c d e f g h
1 2011-04-08 1 1 0 0 0 0 2 0
1 2011-04-09 1 0 0 0 0 0 0 0
2 2011-04-08 0 1 0 0 0 0 1 0
3 2011-04-08 0 0 0 0 0 0 1 0
4 2011-04-09 0 0 0 0 0 1 0 0
*/
select e.empid as [empid],
ISNULL(bc.adate,dc.adate) as [adate],
ISNULL(bc.a,0) as [a],
ISNULL(bc.b,0) as [b],
ISNULL(bc.c,0) as [c],
ISNULL(bc.d,0) as [d],
ISNULL(dc.e,0) as [e],
ISNULL(dc.f,0) as [f],
ISNULL(dc.g,0) as [g],
ISNULL(dc.h,0) as [h]
from employee as e
full join bc on e.empid = bc.empid
full join dc on e.empid = dc.empid