Trying to do a count on logged in users
Trying to do a count on logged in users
(OP)
I am trying to build a query where I can get a count of logged in users. There is a field called eventtype. If it is set to one, then the user is logged in. If it is set to seven, then they are logged out. There are a number of other eventtypes that I can ignore. There is a datetime stamp for each eventtype (see sample data below). I might be able to do this in MS-SQL, but the database is Informix which I am not as well versed in. Any assistance would be appreciated.
Sample Data
UserID DateTime Eventtype
11604 6/27/2016 3.05.53 PM 1 login
11604 6/27/2016 3.05.53 PM 2
11604 6/27/2016 3.06.21 PM 3
11604 6/27/2016 3.07.51 PM 4
11604 6/27/2016 3.08.01 PM 5
11604 6/27/2016 3.11.59 PM 6
11604 6/27/2016 3.16.45 PM 3
11604 6/27/2016 3.20.53 PM 7 logout
11604 6/27/2016 3.25.53 PM 1 login
...etc.
Sample Data
UserID DateTime Eventtype
11604 6/27/2016 3.05.53 PM 1 login
11604 6/27/2016 3.05.53 PM 2
11604 6/27/2016 3.06.21 PM 3
11604 6/27/2016 3.07.51 PM 4
11604 6/27/2016 3.08.01 PM 5
11604 6/27/2016 3.11.59 PM 6
11604 6/27/2016 3.16.45 PM 3
11604 6/27/2016 3.20.53 PM 7 logout
11604 6/27/2016 3.25.53 PM 1 login
...etc.
RE: Trying to do a count on logged in users
select
sum(logged_in) as loggedIn,
sum(not_ready) as notReady,
sum(ready) as Ready,
sum(reserved) as Reserved,
sum(talking) as Talking,
sum(work) as Work
from (select
agentid,
case when eventtype = 2 then count(distinct agentid) end as not_ready,
case when eventtype = 3 then count(distinct agentid) end as ready,
case when eventtype = 4 then count(distinct agentid) end as reserved,
case when eventtype = 5 then count(distinct agentid) end as talking,
case when eventtype = 6 then count(distinct agentid) end as work,
case when eventtype <> 7 then count(distinct agentid) end as logged_in
from agentstatedetail asd
where eventdatetime> today
and eventdatetime = (select max(eventdatetime) from agentstatedetail where asd.agentid = agentstatedetail.agentid)
group by agentid, eventtype
order by agentid, max(eventdatetime) desc)