我有一个LEAVE表如下
EMPID LEAVEDT APPDT LEAVETYPE
------ ------- ------- ---------
E1 10-3-13 5-3-13 CL
E1 11-3-13 5-3-13 CL
E1 12-3-13 5-3-13 CL
E1 13-3-13 5-3-13 SL
E1 14-3-13 5-3-13 SL
E1 15-3-13 5-3-13 PL
E1 16-3-13 5-3-13 CL
E1 17-3-13 5-3-13 CL
E1 18-3-13 5-3-13 PL
E1 19-3-13 5-3-13 PL
E1 20-3-13 5-3-13 CL
现在我想要获取记录,其中我想要显示员工E1每次休假的次数和天数。对于上面的例子,我的查询结果应该是:---
EMPID FROM TO LEAVETYPE
----- ----- ------ ---------
E1 10-3-13 12-3-13 CL
E1 13-3-13 14-3-13 SL
E1 15-3-13 15-3-13 PL
E1 16-3-13 17-3-13 CL
E1 18-3-13 19-3-13 PL
E1 20-3-13 20-3-13 CL
请帮助我使用合适的SQL查询。
请注意:empid
和leavedt
是我桌子的主键。
答案 0 :(得分:0)
对表结构进行少量修改,如下所示。
create table leaves (empid varchar(10) ,
leavefromdt date,
leavetodate date,
appdate date,
leavetype varchar(2));
示例数据
insert into leaves values('E1', Cast('3-10-2013' as datetime),
Cast('3-12-2013' as datetime), Cast('3-5-2013' as datetime),'CL');
insert into leaves values('E1',Cast('3-13-2013'as datetime),
Cast('3-14-2013'as datetime),Cast('5-3-2013'as datetime),'SL');
insert into leaves values('E1',Cast('3-15-2013'as datetime),
Cast('3-15-2013'as datetime),Cast('5-3-2013'as datetime),'PL');
insert into leaves values('E1',Cast('3-16-2013'as datetime),
Cast('3-17-2013'as datetime),Cast('5-3-2013'as datetime),'CL');
查询
select * , DATEDIFF(DAY, leavefromdt,leavetodate )+1 days from leaves
order by empid,leavetype