Sql server 2008 如何从SQL server的两个不同表列中获取一个不同的ListofDate列
有两个表页,包含数据 我需要显示这两个表中不同的ListofDates和employeeid,这两个表的Date列都有一些条件,即在Leave表approvalstatus='Approved'和startdate/enddate期间持续时间中 表架构: 期望输出:Sql server 2008 如何从SQL server的两个不同表列中获取一个不同的ListofDate列,sql-server-2008,tsql,datetime,Sql Server 2008,Tsql,Datetime,有两个表页,包含数据 我需要显示这两个表中不同的ListofDates和employeeid,这两个表的Date列都有一些条件,即在Leave表approvalstatus='Approved'和startdate/enddate期间持续时间中 表架构: 期望输出: 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 201
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
无法将employeeId与日期列表一起获取我认为您不需要分组。这就是你想要的
select idemp_lv, leavedate as ListOfDates
from leaves
where approvalstatus='Approved' and leavedate >='20130302' and leavedate <'20130501'
union
select idemp_at, absentdate as ListOfDates
from Attendanceatd
where absentdate >='20130302' and absentdate <'20130501'
如果使用UNION ALL,则会自动记录差异
除非我遗漏了什么,否则我应该做这件事。只需记住,联合会更快,但会产生更具指令性的sql排序。始终查看执行计划以检查性能。感谢您的promp回复,是的,这很好,实际上我正在与group By进行斗争。就我所知,您能告诉我哪一个使用UNION或UNION ALL的性能更好吗?因为Andrew Jansen的答案也很好。Andrew的答案需要更多的处理。我的组员一步就组成了工会。
select EmpId,ListOfDates
from
(
select idemp_lv as EmpId ,leavedate as ListOfDates from leaves where approvalstatus='Approved'
union all
select idemp_at as EmpId , absentdate as ListOfDates from Attendanceatd
)T1
where ListOfDates between '2013-03-02' and '2013-05-01'
group by EmpId,ListOfDates
select distinct empid, ListOfDates
from
(
select idemp_lv as empid, leavedate as ListOfDates from leaves where approvalstatus='Approved' and leavedate >='20130302' and leavedate <'20130501'
union all
select idemp_at as empid, absentdate as ListOfDates from Attendanceatd where absentdate >='20130302' and absentdate <'20130501'
)t1
group by empid, ListOfDates
select EmpId,ListOfDates
from
(
select idemp_lv as EmpId ,leavedate as ListOfDates from leaves where approvalstatus='Approved'
union all
select idemp_at as EmpId , absentdate as ListOfDates from Attendanceatd
)T1
where ListOfDates between '2013-03-02' and '2013-05-01'
group by EmpId,ListOfDates