Select C.FirstName,C.LastName
, (select count(*) from HWIndependentDevices as D
Inner Join History as X
on D.deviceid=X.link1
and c.recordid=x.link3
and H.t=x.gentime
) as DoorCount
, h.T as Time
, coalesce((select max(Name) from HWIndependentDevices as D
Inner Join History as X
on D.deviceid=X.link1
and c.recordid=x.link3
and H.t=x.gentime
),'Unknown') as DoorMax
, coalesce((select min(Name) from HWIndependentDevices as D
Inner Join History as X
on D.deviceid=X.link1
and c.recordid=x.link3
and H.t=x.gentime
),'Unknown') as DoorMin
from CardHolder as C
Inner Join
(select link3,max(gentime) as t
from History
Where Gentime >= '20100715'
group by link3
) as H
ON C.RecordID = H.Link3
Order by 3 Desc
|