1

I am a beginner in SQL and I hope you people can help me get through this ordeal.

The below data should be grouped by client, Date and User ID (Which I have already achieved).

Client User ID Date Action Module
Moha Mother 01/10/2010 12:35:36 PM Login PP
Moha Voodoo 02/10/2010 05:15:28 PM Login PP
Moha Panther 04/10/2010 04:36:42 PM Login PP
Moha Mother 01/10/2010 12:42:35 PM Some Action PP
Moha Mother 01/10/2010 12:55:14 PM Some Action PP
Moha Voodoo 02/10/2010 06:35:46 PM Login PP
Moha Panther 04/10/2010 04:53:24 PM Some Action PP
Moha Deuce 05/10/2010 09:13:42 PM Login PP
Moha Deuce 05/10/2010 09:19:42 PM Some Action PP
Moha Panther 06/10/2010 08:11:22 PM Login PP
Moha Deuce 05/10/2010 09:27:49 PM Some Action PP
Moha Panther 06/10/2010 08:15:18 PM Some Action PP
Moha Panther 06/10/2010 08:44:53 PM Some Action PP
Moha Deuce 05/10/2010 09:27:49 PM Login PP
Moha Rabbit 05/20/2010 09:27:49 PM Login PP
Moha Voodoo 02/10/2010 06:57:35 PM Some Action PP
Moha Deuce 06/10/2010 08:30:59 AM Login PP
Moha Rabbit 05/21/2010 09:27:49 PM Login PP
Moha Mother 03/10/2010 01:04:54 PM Login PP
Moha Mother 03/10/2010 01:23:55 PM Some Action PP
Moha Mother 03/10/2010 02:25:46 PM Login PP
Moha Mother 03/10/2010 02:45:54 PM Some Action PP

Once that is done I want to know if the below information is achievable through SQL.

Client User ID Date Login Cnt Login Amt (mins) Moha Mother 01/10/2010 1 19.6 (00:06:59 + 00:12:39) - Bracket info not req Moha Mother 03/10/2010 2 39.2 (00:19:01 + 00:20:08) Moha Vodoo … … … Moha … … … …

I have already written a procedure that will dynamically accept optional parameters for client, Start date, End date and UserID. My procedure will first convert the date to varchar format and extract only the date for grouping purposes and then calculate the login count using a case statement. But..!! How do you calculate the time difference between 2 consecutive entries and get the login count with is well under the grouping rules? Is it possible to achieve the same using cursors?

Please feel to ask any questions and give any suggestions on the above mentioned topic as it is one of my critical requirements. Thank you all beforehand.

Thanks CS

2
  • The rows are a little garbled up.. Please format the same in a textpad or an excel sheet to view the rows. Sorry for the inconvenience.
    – user509058
    Nov 16, 2010 at 4:21
  • You can select the lines that you need to format and CTRL+K
    – pavanred
    Nov 16, 2010 at 6:53

1 Answer 1

1

There are multiple solutions to this problem.

First of all, I must say that I cannot give you a complete solution to this problem. The login count is an easy matter, but for the login duration of each user I would require more information. Based on the present data, it is unclear which of the dates signifies the logout time. Assuming that it is the last occurrence of 'Some Action' before there is a new login, a solution would be possible, but I don´t know if that assumption is correct or not. If a user opens multiple sessions in parallel, this assumption would break, and also if the logout time is not recorded (eg. when the session times out after some point and the time is not stored) there is not much you can do.

Anyway, there are three points I want to make:

  • General argument of set-based queries versus cursors
  • Suggested query for login count
  • Hints on how to proceed for the login duration

The first point: you suggest to use cursors and a stored procedure to determine the login count. This solution would work, but it is typically how you would proceed in a normal programming language like for example C or Pascal: define a for-loop over the data and do a calculation or routine for each row. In SQL, it is generally advisable to think differently. Use cursors and loops only when you really have to, and otherwise use a set-based solution. The reason is that set-based queries are easier to optimize for the SQL interpreter. This argument is made convincingly in several articles elsewhere, for example here: Why are relational set-based queries better than cursors?

So how to proceed in a "set-based" way? Starting with the login count, you could do something like:

SELECT user, COUNT(*)
FROM YourTable
WHERE Action = 'Login'
GROUP BY user

This would return the number of logins per user. If you want the number of logins in a certain time period, you can add a date criterium to the WHERE field, and if you want eg. the number of logins per client or per day, you need to add the Client or CAST(Date) AS DATE field to both the SELECT and GROUP BY clauses.

Finally about the login duration, your question is how to calculate the time difference between two consecutive entries and this is indeed how it should be done. In MSSQL, I would do that with ROW_NUMBER() (although there might be better solutions around for your particular case):

WITH NumberedTable AS (
    SELECT User, Date, ROW_NUMBER() OVER (PARTITION BY User ORDER BY Date) AS LoginOccurrenceSortedByDate
    FROM YourTable
    WHERE Action = 'Login'
)
SELECT L.User, L.Date, DATEDIFF(seconds, R.Date, L.Date) AS DifferenceWithPreviousLogin 
FROM NumberedTable L
LEFT JOIN NumberedTable R
ON R.LoginOccurrenceSortedByDate = L.LoginOccurrenceSortedByDate - 1

You basically LEFT JOIN each entry with the previous occurrence and calculate the time difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.