有两张Leaves, Attendanceatd
数据表。
我需要在这两个表中显示不同的ListofDates
和具有某些条件的表,即在表 Date 列上的 Leave table approvalstatus='Approved' 和 startdate / enddate期间持续时间。employeeid
表架构:SQL FIDDLE
create table Leaves(idemp_lv int,leavedate Datetime, approvalstatus varchar(100))
insert into Leaves values (1,'2013-04-22 00:00:00.000','Approved');
insert into Leaves values (2,'2013-04-21 00:00:00.000','Approved');
insert into Leaves values(3,'2013-04-26 00:00:00.000','Approved');
insert into Leaves values(1,'2013-04-21 00:00:00.000','Pending');
insert into Leaves values(3,'2013-04-02 00:00:00.000','Pending');
insert into Leaves values(1,'2013-04-19 00:00:00.000','Approved');
create table Attendanceatd(idemp_at int,absentdate Datetime)
insert into Attendanceatd values(1,'2013-04-19 00:00:00.000');
insert into Attendanceatd values(3,'2013-04-02 00:00:00.000');
insert into Attendanceatd values(1,'2013-04-15 00:00:00.000');
期望的输出:
Empid ListofDate
1 2013-04-22 00:00:00.000
1 2013-04-19 00:00:00.000
1 2013-04-15 00:00:00.000
2 2013-04-21 00:00:00.000
3 2013-04-26 00:00:00.000
3 2013-04-02 00:00:00.000
目前我正在尝试这个:
select ListOfDates
from
(
select leavedate as ListOfDates from leaves where approvalstatus='Approved' and leavedate >='20130302' and leavedate <'20130501'
union
select absentdate as ListOfDates from Attendanceatd where absentdate >='20130302' and absentdate <'20130501'
)t1
group by ListOfDates
无法与ListOfDates一起获取employeeId