我有一个班次数据库,它告诉我个人班次的开始和结束时间,即20/8/2014 07:00:00 to 20/08/2014 15:00:00
但是,我需要弄清楚有多少人在特定时间内轮班,即07:00:00 - 10, 08:00:00 12
等,因为我们需要思考为什么表现可能已经下降等等,是因为我们人手短等?
我可以做约会,看看他们轮班的总小时数,但是我需要将某些计数表放在一起,我想要进行细分,即
Shift 07:00-15:00 - Joe Bloggs
06:00 - 0
07:00 - 1
08:00 - 1
09:00 - 1
14:00 - 1
15:00 - 0
我希望这是有道理的。
卡伦
答案 0 :(得分:1)
在SP中设置此代码并将两个参数作为startdate和enddate传递
CREATE Table #Temp(Datedif varchar(100))
DECLARE @counter int;
select @counter = datediff(hour,'8/20/2013 07:00:00','8/20/2013 15:00:00')
DECLARE @StartHr varchar(50);
SET @StartHr = Substring(Convert(varchar(50),'8/20/2013 07:00:00'),charindex(' ','8/20/2013 07:00:00'),6)
INSERT INTO #Temp(Datedif)VAlues( @StartHr+' - 1')
WHILE @counter > 1
BEGIN
DECLARE @NextHr varchar(50);
SET @NextHr = Substring(@StartHr,1,charindex(':',@StartHr)-1) + 1
IF(@NextHr LIKE '%' + ':' + '%')
BEGIN
INSERT INTO #Temp(Datedif)VAlues( @NextHr+' - 1')
END
ELSE
BEGIN
IF(len(@NextHr) > 1)
BEGIN
INSERT INTO #Temp(Datedif)VAlues( @NextHr+':00 - 1')
END
ELSE
BEGIN
INSERT INTO #Temp(Datedif)VAlues(' 0'+@NextHr+':00 - 1')
END
END
SET @StartHr = @NextHr+':00-1'
SET @counter = @counter - 1;
END
select * from #Temp
DROP TABLE #Temp
答案 1 :(得分:0)
如果您使用的是SQL Server,则可以使用下面的脚本,但类似的逻辑可以应用于其他DBMS
DECLARE
@currentDate datetime;
SET @currentDate = GETDATE( );
-- Hoursly count for last X hours
DECLARE
@counter int;
SET @counter = <Hr Diff>;
WHILE @counter > 0
BEGIN
DECLARE
@startTime datetime;
SET @startTime = DATEADD( HOUR , -@counter , @currentDate );
DECLARE
@endTime datetime ;
SET @endTime = DATEADD( HOUR , -(@counter - 1), @currentDate );
-- Fetch count from your table <Business Logic>
select @startTime, @endTime COUNT( <Entity> ) from tableName BETWEEN @startTime AND @endTime;
SET @counter = @counter - 1;
END;