Unix Technical Forum

SQL question

This is a discussion on SQL question within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi all, I have the following table to log events as they happen: EventID UserID Event Time ====== ===== ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:20 PM
haroldsphsu@gmail.com
 
Posts: n/a
Default SQL question

Hi all,

I have the following table to log events as they happen:

EventID UserID Event Time
====== ===== ==== ====
1 User 1 Login 08:00:00
2 User 1 Logout 11:00:00
3 User 1 Login 13:00:00
4 User 1 Logout 16:00:00

Is there a way to transform it into the following form?

UserID Login Logout
===== ==== =====
User 1 08:00:00 11:00:00
User 1 13:00:00 16:00:00

I understand I can log the start and end time instead, but I was just
wondering if the above is possible?

Thanks,
Harold

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:20 PM
Erland Sommarskog
 
Posts: n/a
Default Re: SQL question

(haroldsphsu@gmail.com) writes:
> I have the following table to log events as they happen:
>
> EventID UserID Event Time
>====== ===== ==== ====
> 1 User 1 Login 08:00:00
> 2 User 1 Logout 11:00:00
> 3 User 1 Login 13:00:00
> 4 User 1 Logout 16:00:00
>
> Is there a way to transform it into the following form?
>
> UserID Login Logout
>===== ==== =====
> User 1 08:00:00 11:00:00
> User 1 13:00:00 16:00:00
>
> I understand I can log the start and end time instead, but I was just
> wondering if the above is possible?


SELECT a.UserID, a.Login, b.Logout
FROM tbl a
JOIN tbl b ON a.UserID = b.UserID
AND b.EventID = (SELECT MIN(c.EventID)
FROM tbl c
WHERE c.UserID = a.UserID
AND c.Event = 'Logout'
AND c.Time > a.Time)
WHERE a.Event = 'Login'

However, this presumes that all logins are followed by logouts and
vice versa. With imperfect data, the output can be funky.

I've also assumed that the real-world data has date as well as time.
If you only have time, some different rules need to be found for
sessions across midnight.

For this type questions, it is always a good idea to post:

o CREATE TABLE statements for your tables.
o INSERT statements with sample data.
o The desired output given the sample.
o A short narrative describing the business problem .

This makes it easy to copy and paste into a query tool, and develop a
tested solution.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:20 PM
haroldsphsu@gmail.com
 
Posts: n/a
Default Re: SQL question


Thanks Erland! And yes, I'll remember to post the scripts next time.

Thanks again.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:21 PM
 
Posts: n/a
Default Re: SQL question


Uzytkownik <haroldsphsu@gmail.com> napisal w wiadomosci
news:1139346932.755113.118540@f14g2000cwb.googlegr oups.com...
> Hi all,
>
> I have the following table to log events as they happen:
>
> EventID UserID Event Time
> ====== ===== ==== ====
> 1 User 1 Login 08:00:00
> 2 User 1 Logout 11:00:00
> 3 User 1 Login 13:00:00
> 4 User 1 Logout 16:00:00
>
> Is there a way to transform it into the following form?
>
> UserID Login Logout
> ===== ==== =====
> User 1 08:00:00 11:00:00
> User 1 13:00:00 16:00:00
>
> I understand I can log the start and end time instead, but I was just
> wondering if the above is possible?


Change the table to:

EventID UserID Login Logout
=========================

then remember EventID after Login and update the row
after Logout.

regards



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:38 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com