I have an application that will be logging to a SQL Server 2000
database user user activity from several Windows 2003 terminal
servers. This information will be retrieved by monitoring the
Security logs of these servers (this part I know how to accomplish
already).
A table in the database, tblLogEntries, will contain the following
fields:
- ID = autoincrementing int
- LogTime = Date/Time the user activity was recorded in the security
log
- Username = User's login ID that the activity was recorded with
- Type = int, referencing a lookup table with the values of Logon,
Logoff, and possible other future items
- Server = The name of the server the activity was recorded on.
The only question I have is, can you offer a way to process the total
user login time during a given range using T-SQL.
For Example...
Given the table data:
ID LogTime Username Type Server
1 10-10-2003 8:30:00 Tom Logon SERVER-A
2 10-10-2003 8:45:00 Sarah Logon SERVER-A
3 10-10-2003 16:45:00 Tom Logoff SERVER-A
4 10-10-2003 17:00:00 Sarah Logoff SERVER-A
5 10-11-2003 8:30:00 Tom Logon SERVER-A
6 10-11-2003 8:45:00 Sarah Logon SERVER-A
7 10-11-2003 16:30:00 Sarah Logoff SERVER-A
8 10-11-2003 17:15:00 Tom Logoff SERVER-A
How would you receive the output:
User Logon Total Time for SERVER-A
Tom 17.0 hrs
Sarah 16.0 hrs
I know I can handle this type of processing on my ASP.NET front-end,
but I'm curious as to how easily it can be done by the database,
itself.
Thanks in advance for your assistance.Do:
SELECT UserName,
SUM( DATEDIFF( hour, Login, COALESCE( Logoff, Login ) ) )
FROM ( SELECT t1.Username, t1.LogTime,
( SELECT MIN( t2.LogTime )
FROM tbl t2
WHERE t2.Server = t1.Server
AND t2.UserName = t1.Username
AND t2.Type = 'LogOff'
AND t2.LogTime > t1.LogTime )
FROM tbl t1
WHERE t1.Type = 'Logon'
AND t1.Server = 'SERVER-A' ) D ( UserName, Login, Logoff )
GROUP BY UserName ;
--
- Anith
( Please reply to newsgroups only )|||Thanks for the reply.. the query looks good, but I'm having a little
trouble following it, and, as a result, cannot get it to work.
Would you mind explaining it a bit or point me to a reference for this
type of processing?
I'm not sure what that "D" operator is for, and I'll read up on the
COALESCE in BOL.
------------
http://members.tripod.com/kcourville0/
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||D is an alias used for a derived table used in the query; when you use a
subquery construct directly in the FROM clause of an SQL statement with an
alias, in t-SQL, it is called a derived table. Pl. refer to SQL Server Books
Online for syntax and more details on this construct.
The logic is simple:
1. Retrieve the list of users with their login times and the subsequent
logout time (this is achieved using a subquery with MIN function). You can
run the derived table by itself to further understand how it is evaluated.
2. With the data in the derived table, the outer query, get the time
difference between the login time and the logout times and then find the
total using the SUM function.
--
- Anith
( Please reply to newsgroups only )|||Cool... I'll have to learn more of derived tables.
Thanks again for your response.
------------
http://members.tripod.com/kcourville0/
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
No comments:
Post a Comment