You can do something like what I did below. I'm not 100% clear about what you are trying to do but this should help....
You would have an agent job run frequently (say, every minute) and populate a login table. Then you would create a view against the table that collects the login info (I'm using a temp table called #loginInfo in my example). You could use that view for whatever metrics you are looking for.
-- (1) Create a job that collects login info like so (this would need to run frequently)
IF OBJECT_ID('tempdb..#loginInfo') IS NULL
CREATE TABLE #loginInfo
(event_id int identity primary key,
DBName varchar(100) not null,
LoginName varchar(100) not null,
LoginTime smalldatetime);
-- (2) variable or parameter for the db you want to track
DECLARE @myDB varchar(100)='ajbTest'
IF NOT EXISTS (SELECT * FROM #loginInfo)--If the table is empty
INSERT INTO #loginInfo (DBName, LoginName, LoginTime)
SELECTDB_NAME(dbid) AS DBName,
loginame AS LoginName,
CAST(login_time AS smalldatetime) AS LoginTime
FROM sys.sysprocesses
WHERE DB_NAME(dbid)=@myDB
GROUP BY dbid, loginame, CAST(login_time AS smalldatetime)
ELSE --if the table is not empty
INSERT INTO #loginInfo (DBName, LoginName, LoginTime)
SELECTDB_NAME(dbid) AS DBName,
loginame AS LoginName,
CAST(login_time AS smalldatetime) AS LoginTime
FROM sys.sysprocesses
WHERE DB_NAME(dbid)=@myDB
GROUP BY dbid, loginame, CAST(login_time AS smalldatetime)
EXCEPT
SELECTDBName,
LoginName,
LoginTime
FROM #loginInfo;
GO
--Captured data (included for review)
SELECT * FROM #loginInfo
-- (3) Create a view that tracks connections between 9AM and 5PM
SELECT DBName, LoginName, COUNT(DBName) AS TotalConnections
FROM #loginInfo
WHERE DATEPART(HOUR, loginTime)>=9 AND DATEPART(HOUR, loginTime)<=17
GROUP BY DBName, LoginName
Again, this is not perfect but my get you in the right direction.
-- Itzik Ben-Gan 2001