473,419 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

How to group in a left join

Hey forum. Not a SQL newbie here but new to the forum. A somewhat advanced query has my stumped. I have a table of session information that provides log-ons and log-off times like this (column names are whenlogged, userid, msg):
Expand|Select|Wrap|Line Numbers
  1. 10/23/2010-8:21:46-AM    10    mimmy (10) started session
  2. 10/23/2010-8:07:21-AM    12    mark (12) started session
  3. 10/22/2010-2:38:37-PM    12    mark (12) started session
  4. 10/22/2010-2:27:21-PM    32    grace (32) ended session
  5. 10/22/2010-2:11:01-PM    32    grace (32) started session
  6.  
I need to generate a report showing the session log-ons and log-offs, ordered by desc log-on date/time, something like this:
Expand|Select|Wrap|Line Numbers
  1. userid     session start     session end
  2. 10     10/23/2010-8:21:46-AM    ?
  3. 12     10/23/2010-8:07:21-AM    ?
  4. 12     10/23/2010-2:38:37-PM    ?
  5. 32     10/23/2010-2:11:01-PM    10/22/2010-2:27:21-PM
  6.  
I am stuck at this left-join:
Expand|Select|Wrap|Line Numbers
  1. select starts.userid,master.accountfullname(starts.userid ) as "user",starts.whenlogged as started, stops.whenlogged as stopped
  2. from master.sessionlog starts left join master.sessionlog stops
  3. on starts.userid=stops.userid and starts.whenlogged<stops.whenlogged
  4. where starts.msgtype=0 and starts.userid<>0 and starts.msg like '% started session'
  5. and stops.msgtype=0 and stops.userid<>0 and stops.msg like '% ended session'
  6. order by starts.whenlogged desc
  7.  
...since I cannot figure how to group the users in such a way as to take the closest log-off time after the log-on time for each user. The query should also gracefully handle the many cases where a user does not properly end the session (shown as ? in a report).

Can any SQL guru out there help?

Thanks,
Mark
Oct 27 '10 #1
1 1604
ck9663
2,878 Expert 2GB
First, create your sample data.....

Expand|Select|Wrap|Line Numbers
  1.  
  2. drop table session;
  3.  
  4. with test_data 
  5. as
  6. (
  7. select cast('10/23/2010 8:21:46 AM' as datetime) as whenlogged,    10 as userid,    'mimmy (10) started session' as msg
  8. union all
  9. select '10/23/2010 8:07:21 AM',    12,    'mark (12) started session'
  10. union all
  11. select '10/22/2010 2:38:37 PM',    12,    'mark (12) started session'
  12. union all
  13. select '10/22/2010 2:27:21 PM',    32,    'grace (32) ended session'
  14. union all
  15. select '10/22/2010 2:11:01 PM',    32,    'grace (32) started session' 
  16.  
  17. select 
  18. *
  19. into session
  20. from test_data
  21.  
  22. select * from session
  23.  
  24.  
Here's the code that will summarize your session table. You're going to have to convert the datetime column to varchar if you really want your query to return "?" for the missing end session. This query defaults it to NULL, instead.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. ;with sorted_session
  5. as 
  6. (
  7.    select 
  8.    row_cnt = row_number() over(partition by userid order by whenlogged),
  9.    whenlogged, userid, msg
  10.    from session
  11. select 
  12. logged_in.userid, logged_in.whenlogged as session_start, 
  13. session_id = 
  14. case 
  15.    when logged_out.row_cnt is null then null
  16.    else logged_out.whenlogged
  17. end
  18. from sorted_session logged_in
  19.    left join sorted_session logged_out on logged_in.userid = logged_out.userid and logged_in.row_cnt + 1 = logged_out.row_cnt and logged_out.msg like '%ended session%'
  20. where logged_in.msg like '%started%'
  21.  
  22.  
Just a suggestion, make the start/end session indicator as a code or a flag, not a string...

Happy Coding!!!


~~ CK
Oct 27 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: StealthBananaT | last post by:
My database has two tables - films has 10,000 records and reviews has 20,000 records. Whenever I try to list all the films and the count of its reviews, MySQL locks and I have to restart the...
0
by: Jon Trelfa | last post by:
I'm having difficulties getting all rows to return from the left table on a left join. For some reason, i only get the rows where there is a match between the right and the left table. Here's...
1
by: Quarco | last post by:
Hi, Suppose I have a query like: SELECT products.name AS product, SUM(IF(stock.invoice=0,1,0)) AS in_stock, SUM(IF(shopcart.status=1,1,0)) AS reserved FROM products LEFT JOIN stock ON...
1
by: rossz | last post by:
I have this query to get the 10 most common products from in stock, based upon which parent category they are in: SELECT p.*,i.sql_inventory AS quantity FROM products AS p JOIN inv_cnt AS i ON...
7
by: deko | last post by:
SELECT tblTxAcct.TxAcctName, tblTxType.TxTypeName, Nz(qryTxAcctListCt.TxCount, 0) AS TxCt FROM (tblTxAcct INNER JOIN tblTxType ON tblTxAcct.TxType_ID=tblTxType.TxType_ID) LEFT JOIN qryTxAcctListCt...
14
by: Ryan | last post by:
I want to do the following SQL statement in Access. However, it won't allow me to have the secondary part of my join statement and tells me that this is not supported. OK, so Access doesn't support...
2
by: Darryl Kerkeslager | last post by:
The following SQL does as I intend, but ... SELECT offender_id, off_name, inv_ppo_id, add_note, add_zip_id, it_name AS Purpose FROM (offender INNER JOIN (investigation INNER JOIN...
2
by: David F | last post by:
Hello, I have what seems like a simple left join query, but Access returns results like it's an inner join. I've tried numerous combinations without success. I have a table (ProjectList)...
5
by: jim | last post by:
Hi, I've browsed several posts, but still haven't found the answer I'm looking for. I have one table (A) that contains a list of values I want to return. I have two other tables (B) and (C)...
4
by: polycom | last post by:
Assistance needed to optimize this query SELECT SD.content_id AS Id, SD.title AS Title, CT.name AS Contenttype, PV.content_id AS SponsorId, ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.