Solved

Self Join to multiple tables MS SQL server

Posted on 2012-12-29
10
446 Views
Last Modified: 2012-12-31
Question:

I need to add additional columns 'Total Locked time' and 'Total Remote Connect' for EventTypeID's 5&6, 7&8 respectively.

I have based the current solution on this article:

http://www.sqlservercentral.com/Forums/Topic693606-8-1.aspx

Background

Using MS SQL server have created the following table to store user login, logout, remote connect, remote disconnect, workstation lock and unlock:

CREATE TABLE [dbo].[DetailedDeviceEvents](
[ID] [bigint]
IDENTITY(1,1) NOT NULL,
[DeviceID] [int] NOT NULL,
[ScheduleID] [bigint]NOT NULL,
[EventTypeID] [int] NOT NULL,
[EventTime] [datetime] NOT NULL,
[CreatedBy] [int] NOT NULL,
[CreatedDateTime] [datetime]NOT NULL,
[RemoteDeviceID] [int] NULL,
[via] [varchar] (50) NULL,
[ntlogin] [varchar] (100) NULL

Open in new window


When I execute the following script the output below is generated.


SELECT
t1.ntlogin ntlogin,
t2.eventTime LogoutTime,
MIN(DateDiff(n, t1.Eventtime, t2.Eventtime)) [LoginDuration(Mins)]
FROM DetailedDeviceEvents t1
INNER join DetailedDeviceEvents t2
ON t1.ntlogin = t2.ntlogin
AND t1.EventTypeID = 3
AND t2.EventTypeID = 4
AND t1.EventTime < t2.EventTime

WHERE t2.EventTime between '2012-11-01' and '2012-12-19'
GROUP BY t1.ntlogin, t2.EventTime

Open in new window


Example output
ntlogin     LogoutTime                          LoginDuration(Mins)
jjones     2012-11-01   13:09:41.450      276
ssmith    2012-11-01   18:28:01.370      547
ssmith    2012-11-02   12:48:43.583      222
ssmith    2012-11-02   16:01:54.837     190
ssmith    2012-11-05   17:06:45.393     489
Xmoanne 2012-11-06   14:31:08.657    306
ssmith    2012-11-06   18:06:09.493     534
tevans    2012-11-07   17:29:11.527      373
ssmith    2012-11-07   17:31:36.327     528
Xmoanne 2012-11-08   14:29:43.597    295
ssmith    2012-11-08   18:48:02.197     638
Xmoanne 2012-11-09 14:28:27.373      289

I could use a UNION statement for the remaining two banks of EventTypeID's (5&6, 7&8) and concatenate the output, but that wouldn't give me the individual column totals.

What I need is a solution that provides the additional two total columns, I believe that this can be accomplished by using additional self referencing tables; I have tried several additional inner joins to additional tables without success. I will be running this on SQL2008 or SQL 2012, so if there are any additional TSQL functions that could make the task easier then feel free to propose. I looking for speed an efficiency as  there will be many thousands of rows. Indexing will need to be added to the 'ntlogin' and date columns in order to improve performance, but in the first instance I require help to solve the problem.

Please let me know if you require additional information.

Thanks
0
Comment
Question by:BJM1M
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 38730695
What exactly do you expect? You need common criteria to show aggregated data of different sources (like the event types here). It does not make sense to display a "Logout Time" together with a "Total Locked time" - that is, if you do not mean to display the overall total for the user, which would lead to a repeated value for each logout event.
0
 

Author Comment

by:BJM1M
ID: 38730762
What I expect is:

Example output
ntlogin     LogoutTime                          LoginDuration(Mins) Locked time  Remote time
jjones     2012-11-01   13:09:41.450      276                         123                  55
ssmith    2012-11-01   18:28:01.370      547                         76                     7
ssmith    2012-11-02   12:48:43.583      222                         55                    25

If run the following code - note the change for the EventTypeID's - 5&6 i.e. Lock/Unlock, I receive data as shown above for the Lock/Unlock events, similarly for the Remote Connect/Disconnect.

Lock & Unlock
SELECT
t1.ntlogin ntlogin,
t2.eventTime LogoutTime,
MIN(DateDiff(n, t1.Eventtime, t2.Eventtime)) [LoginDuration(Mins)]
FROM DetailedDeviceEvents t1
INNER join DetailedDeviceEvents t2
ON t1.ntlogin = t2.ntlogin
AND t1.EventTypeID = 5 -- Lock
AND t2.EventTypeID = 6 -- Unlock
AND t1.EventTime < t2.EventTime

WHERE t2.EventTime between '2012-11-01' and '2012-12-19'
GROUP BY t1.ntlogin, t2.EventTime

Open in new window


Remote Connect
SELECT
t1.ntlogin ntlogin,
t2.eventTime LogoutTime,
MIN(DateDiff(n, t1.Eventtime, t2.Eventtime)) [LoginDuration(Mins)]
FROM DetailedDeviceEvents t1
INNER join DetailedDeviceEvents t2
ON t1.ntlogin = t2.ntlogin
AND t1.EventTypeID = 7 -- Remote Connect
AND t2.EventTypeID = 8 -- Remote Disconnect
AND t1.EventTime < t2.EventTime

WHERE t2.EventTime between '2012-11-01' and '2012-12-19'
GROUP BY t1.ntlogin, t2.EventTime

Open in new window


So, what I want is to add the output from the code above to the overall script so as to produce the Lock/Unlock  Remote connection duration for the ntlogin.

Hopefully this answers your question?
0
 

Author Comment

by:BJM1M
ID: 38730766
Just one further point, if by showing the Logout time causes issues, then it would be fine to show the totals for each category, i.e. LoginDuration(Mins), Locked time,  Remote time.

Thanks
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 70

Accepted Solution

by:
Qlemo earned 100 total points
ID: 38730777
The point is, as I said already, you need common criteria to summarize. The logout time will not fit to lock or remote event timestamps, I assume, so we can't match those event types.
Leaving the date aside, we only had the login, and that would allow for a single row per login only.
I understand well that you can get all info isolated from each other - you have a time stamp for each event type, which makes it unique. But putting together related info is difficult. You will need a rule, like the lock events before login, but after prior logout.
0
 

Author Comment

by:BJM1M
ID: 38730806
OK. If it cannot be achieved in the way that I require I will run three separate blocks to get what I need.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 38731445
Seems as if you did not fully understand how the grading system on EE works.
The "C" grade is inappropriate because the answer given was with best effort and knowledge. "You can't do that" can be a valid answer, and then needs to be graded at least with a "B" (but usually is with an "A" - nothing to improve about the answer).
0
 

Author Comment

by:BJM1M
ID: 38731669
OK so what's the process to re-grade?
0
 

Author Comment

by:BJM1M
ID: 38732378
Yes please. Please reassign to A.

Thanks

apologies for any confusion.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question