2015-12-21 81 views
1

我有一個簡單的表格,可以記錄人員進入時鐘和進入時鐘狀態。通過一組值(入住或退房)

Id  | EmployeeNumber | InOutId | InOutDateTime 
----------------------------------------------------- 
1  | 505   | IN  | 2015-03-24 08:32:42:000 
1  | 506   | IN  | 2015-03-24 08:35:47:000 
1  | 507   | IN  | 2015-03-24 08:46:12:000 
1  | 505   | OUT  | 2015-03-24 16:59:00:000 
1  | 506   | OUT  | 2015-03-24 17:05:00:000 

我想顯示當前總人數和當前出的人數。換句話說: - Total IN表示那些在給定日期沒有相應OUT的人。 - 總OUT表示那些在某一天有IN和OUT的人。

因此,基於以上我的表,我希望得到以下結果:

TotalCurrentlyIn  | TotalCurrentlyOut 
----------------------------------------- 
1     | 2 

這是我到目前爲止有:

DECLARE @d date; 
set @d = cast('2015-03-24 15:02:42.000' as date) 

select EmployeeNumber, InOutId, InOutDateTime from MyAttendance 
where 
    InOutDateTime >= DATEADD(day, DATEDIFF(day, 0, @d), 0) 
    and InOutDateTime < DATEADD(day, DATEDIFF(day, 0, @d) +1, 0) 
order by 
    EmployeeNumber, InOutId 

我需要能夠總結和分組 - 由任何想法?

+1

使用的標記dbms。 (看起來不像ANSI SQL ...) – jarlh

回答

0

CheckIn = 1和CheckOut = 2因此您需要檢查所有用途的最後一項。

Select EmployeeId, ActionType, Max(ActionDateTime) From AttendanceLog Where ActionDateTime >= DATEADD(day, DATEDIFF(day, 0, @d), 0) and ActionDateTime < DATEADD(day, DATEDIFF(day, 0, @d) +1, 0) Group by EmployeeId, ActionType Order by EmployeeId,ActionType

0

如果我理解這個問題。你需要知道有多少人是在辦公室現在:

第一個查詢返回的最大日期爲任何僱員,比你用的操作類型

select 
EmployeeId , max(ActionDateTime) as MaxActionDateTime into #temptable 
from table 
group by EmployeeId 


select count (EmployeeId), ActionType 
from table inner join #temptable 
on table.EmployerId == #temptable.EmployerId 
    and table.ActionDateTime == #temptable.MaxActionDateTime 
group by ActionType 
+0

爲了避免性能方面的問題,最好避免使用臨時表 - 您可以在不使用臨時表的情況下提供替代方案嗎? – AshesToAshes

+1

你可以在select中使用select,把第一個查詢(選擇..)作爲tbl並加入並從所有表中選擇 – Racheli

+0

它的工作,謝謝,我想根據日期過濾它。 – Faizan

1

嘗試加入,

DECLARE @d date; 
    set @d = cast('2015-03-24 15:02:42.000' as date) 

    ;with cte as(
    select t.EmployeeNumber,t.InOutId as in1, 
    t1.InOutId out1,t.InOutDateTime from @t t 
    left join (select EmployeeNumber,InOutId,InOutDateTime from @t 
    where InOutId='OUT' and cast(InOutDateTime as date)=cast(@d as date)) t1 
     on t.EmployeeNumber=t1.EmployeeNumber and 
     cast(t.InOutDateTime as date)=cast(t1.InOutDateTime as date) 
      where t.InOutId='IN' and cast(t.InOutDateTime as date)=cast(@d as date)) 
    select count(in1) Totalin,count(out1) Totalout, sum(case when out1 is null then 1 else 0 end) TotalCurrentlyIn 
    ,count(out1) TotalCurrentlyOut from cte 

數據

declare @t table (Id int,EmployeeNumber int, InOutId varchar(3), InOutDateTime datetime) 
insert into @t(Id, EmployeeNumber,InOutId, InOutDateTime) values 
(1  , 505   , 'IN'  , '2015-03-24 08:32:42:000'), 
(1  , 506   , 'IN'  , '2015-03-24 08:35:47:000'), 
(1  , 507   , 'IN'  , '2015-03-24 08:46:12:000'), 
(1  , 505   , 'OUT'  , '2015-03-24 16:59:00:000'), 
(1  , 506   , 'OUT'  , '2015-03-24 17:05:00:000') 
+0

它未給出在該特定日期內進入或退出的員工數,其顯示爲特定員工 – Faizan

+0

員工來了,我想要在特定日期內進出的員工數量...就像12月21日有多少empyess出來,並在 – Faizan

+0

'total in'和'total out'已經添加到解決方案中,謝謝 – nazark

0

使用窗口功能,你可以得到每emplo的最後一個動作ye數數

With data As (
    Select id, EmployeeNumber, InOutId 
     , lastAction = ROW_NUMBER() OVER (PARTITION BY EmployeeNumber 
             ORDER BY InOutDateTime DESC) 
    From table1 
) 
Select Count(CASE InOutId WHEN 'IN' THEN 1 END) TotalCurrentlyIn 
    , Count(CASE InOutId WHEN 'OUT' THEN 1 END) TotalCurrentlyOut 
From data 
Where lastAction = 1