日期:2014-05-18  浏览次数:20575 次

查找出某一天没有工作,求解。
表名incident:编号,员工名称,工作名称,开始时间,结束时间。
SQL code

select ID,Name,WorkName,RBTime,RFTime from incident


这张表中保存的是某一员工做某项工作的工作时间表。我想查的是某一员工本月中那一天没有事干。大虾们帮下忙新手。

------解决方案--------------------
有无可能时间相互嵌套的?

上个工作1号到5号,
下个工作3号到10号.

是否会这种情况?
一个工作,是否可以跨天
------解决方案--------------------
这个问题应该是求间断问题
http://topic.csdn.net/u/20120325/17/5a53bd46-8870-450a-a9ca-7ef8661e638d.html?1464
------解决方案--------------------
create table incident
(编号 int ,
员工名称 varchar(100),
工作名称 varchar(100),
开始时间 datetime ,
结束时间 datetime)
go

delete from incident

insert into incident
select 1,'aaa','a','2011-3-5','2011-03-06' union 
select 1,'aaa','b','2011-3-5','2011-03-07' union 
select 1,'aaa','cb','2011-3-9','2011-03-10' union 
select 1,'aaa','d','2011-3-11','2011-03-12' 

select * 
from incident t
where not exists (select 1 from incident where 编号=t.编号 and t.结束时间 between 开始时间-1 and 结束时间 and 工作名称!= t.工作名称)

结果:
1 aaa b 2011-03-05 00:00:00.000 2011-03-07 00:00:00.000
1 aaa d 2011-03-11 00:00:00.000 2011-03-12 00:00:00.000
------解决方案--------------------
SQL code

select ID,Name,WorkName,RBTime,RFTime from incident
create table incident(ID int,Name varchar(32),WorkName varchar(32),RBTime datetime,RFTime datetime)
go
insert into incident select 1,'ss','sad','2012-03-01','2012-03-01' union all

select 2,'ss','sad','2012-03-02','2012-03-02' union all
select 3,'ss','sad','2012-03-03','2012-03-03'



declare @month_day int
set @month_day=datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' as datetime))))
declare @month_first_day datetime
set @month_first_day= convert(varchar(10),dateadd(dd,-DatePart(day,dateadd(dd,-1,getdate())),getdate()),120)

select a.month_day,b.* from (
select convert(varchar(10),dateadd(day,number,@month_first_day),120)as month_day from master.dbo.spt_values where type='p' and number <=@month_day)a
left join incident as b on a.month_day=b.RBTime

/*
month_day    ID    Name    WorkName    RBTime    RFTime
2012-03-01    1    ss    sad    2012-03-01 00:00:00.000    2012-03-01 00:00:00.000
2012-03-02    2    ss    sad    2012-03-02 00:00:00.000    2012-03-02 00:00:00.000
2012-03-03    3    ss    sad    2012-03-03 00:00:00.000    2012-03-03 00:00:00.000
2012-03-04    NULL    NULL    NULL    NULL    NULL
2012-03-05    NULL    NULL    NULL    NULL    NULL
2012-03-06    NULL    NULL    NULL    NULL    NULL
2012-03-07    NULL    NULL    NULL    NULL    NULL
2012-03-08    NULL    NULL    NULL    NULL    NULL
2012-03-09    NULL    NULL    NULL    NULL    NULL
2012-03-10    NULL    NULL    NULL    NULL    NULL
2012-03-11    NULL    NULL    NULL    NULL    NULL
2012-03-12    NULL    NULL    NULL    NULL    NULL
2012-03-13    NULL    NULL    NULL    NULL    NULL
2012-03-14    NULL    NULL    NULL    NULL    NULL
2012-03-15    NULL    NULL    NULL    NULL    NULL
2012-03-16    NULL    NULL    NULL    NULL    NULL
2012-03-17    NULL    NULL    NULL    NULL    NULL
2012-03-18    NULL    NULL    NULL    NULL    NULL
2012-03-19    NULL    NULL    NULL    NULL    NULL
2012-03-20    NULL    NULL    NULL    NULL    NULL
2012-03-21    NULL    NULL    NULL    NULL    NULL
2012-03-22    NULL    NULL    NULL    NULL    NULL
2012-03-23    NULL    NULL    NULL    NULL    NULL
2012-03-24    NULL    NULL    NULL    NULL    NULL
2012-03-25    NULL    NULL    NULL    NULL    NULL
2012-03-26    NULL    NULL    NULL    NULL    NULL
2012-03-27    NULL    NULL    NULL    NULL    NULL
2012-03-28    NULL    N