How to find total login times for a user?

bds212

Every time a user logs in to my application, a new sessionID is created that is unique to the login. If a user logs in once, logs out, and then logs back in they will have a different sessionID.

How do I find the total time that each user has been logged in over the last two weeks?

My table has UserName, sessionID, and timestamp columns, and a row is inserted into the table for each action performed. I want to sum the difference between the first instance of the sessionID and the last instance of the sessionID for each session for a user during the last two weeks. Actions outside of the two week time period can be ignored.

Example data:

UserName sessionID timestamp 
user1    s1        '2019-07-01 08:00:00.000' 
user1    s1        '2019-07-01 10:00:00.000' 
user2    a1        '2019-07-01 16:00:00.000'
user1    s2        '2019-07-02 08:00:00.000' 
user2    a1        '2019-07-02 09:00:00.000'
user1    s2        '2019-07-02 10:00:00.000' 

Example result:

UserName LoginTime
user1    04:00:00.000
user2    17:00:00.000

I was thinking of mapping a UserName to every sessionID that was associated with the UserName and then finding the max and min timestamp for each and summing them up, but that would require partitions in which I am hopelessly lost.

Ryan Prechel

Based on your example data:

  • Both the Username and SessionId columns have a character data type, which I will mimic using NVARCHAR(80).
  • The Timestamp column has a date and time data type, which I will mimic using DATETIME.

The following SQL creates an example table, populates the table with example data, and calculates the sum of the number of minutes for each session for each user from the last two weeks.

CREATE TABLE Session
(
    Username NVARCHAR(80) NOT NULL,
    SessionId NVARCHAR(80) NOT NULL,
    Timestamp DATETIME NOT NULL
);

DECLARE @date DATETIME = DATEADD(DAY, 15, DATEDIFF(DAY, 0, GETDATE()));

INSERT INTO Session(Username, SessionId, Timestamp) VALUES
('user1', 's1', DATEADD(HOUR, 8, @date)),
('user1', 's1', DATEADD(HOUR, 10, @date)),
('user1', 's2', DATEADD(HOUR, 32, @date)),
('user1', 's2', DATEADD(HOUR, 34, @date)),
('user1', 's3', DATEADD(HOUR, 56, @date)),
('user1', 's3', DATEADD(HOUR, 58, @date)),
('user1', 's4', DATEADD(HOUR, 80, @date)),
('user1', 's4', DATEADD(HOUR, 82, @date)),
('user2', 's5', DATEADD(HOUR, 81, @date)),
('user2', 's5', DATEADD(HOUR, 82, @date)),
('user2', 's6', DATEADD(HOUR, 86, @date)),
('user2', 's6', DATEADD(HOUR, 87, @date));


SELECT Username, SUM(SessionTime)
FROM
(
    SELECT Username, SessionId, DATEDIFF(MINUTE, MIN(Timestamp), MAX(Timestamp)) AS SessionTime
    FROM Session
    WHERE Timestamp > DATEADD(WEEK, -2, GETDATE())
    GROUP BY Username, SessionId
) AS S1
GROUP BY Username;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to find total filesize grouped by extension

分類Dev

How to find only the total RAM in Python

分類Dev

How to login user to a google webpage after successful OAuth 2.0 login?

分類Dev

"Last" command: How to show latest user login?

分類Dev

How to request user login in models or views

分類Dev

How to show user login name on the next page

分類Dev

How to get User session in Login handling in Django?

分類Dev

How to disable the login password for a specific user?

分類Dev

How do I store total points after user input in Javascript?

分類Dev

if first user already login, how to prevent second user login on another tab

分類Dev

Using Canvas, how to draw a shape X-times entered by the user?

分類Dev

Using user input to determine how many times a for loop iterates

分類Dev

How to find all the groups the user is a member? (LDAP)

分類Dev

How to prevent user to go to login page after logged in in Vuejs?

分類Dev

How to select all data from table if user login correct flutter

分類Dev

How do I redirect a user after login with fetch()

分類Dev

how to call a plugin after user login in joomla 3.0

分類Dev

How to check if a user of my web application is trying to login with different credentials?

分類Dev

How The Spring Security is redirecting to Login page if User is unauthorized

分類Dev

how to define a function that counts how many times the words from 'the LM word' list (all words in total) appear in the text?

分類Dev

Laravel login as another user

分類Dev

How can I check a user from wp_user table on a custom login page?

分類Dev

How do I calculate the total sum of my cart to my dictionary list based on user input menu?

分類Dev

How does one write an SQL query to add up a total of balances for each user's most recent transaction?

分類Dev

How do i find the total number of subjectsthat has no prerequisites using agregation?

分類Dev

Websphere MQ- How to find the total number of messages that pass througha queue manager

分類Dev

Find how many times a table must be searched to get a match Oracle database

分類Dev

How to find UNIX times corresponding to a particular date in a very large file in R

分類Dev

How can I find out the number of times an RSK transaction has been confirmed on the RSK blockchain?

Related 関連記事

  1. 1

    How to find total filesize grouped by extension

  2. 2

    How to find only the total RAM in Python

  3. 3

    How to login user to a google webpage after successful OAuth 2.0 login?

  4. 4

    "Last" command: How to show latest user login?

  5. 5

    How to request user login in models or views

  6. 6

    How to show user login name on the next page

  7. 7

    How to get User session in Login handling in Django?

  8. 8

    How to disable the login password for a specific user?

  9. 9

    How do I store total points after user input in Javascript?

  10. 10

    if first user already login, how to prevent second user login on another tab

  11. 11

    Using Canvas, how to draw a shape X-times entered by the user?

  12. 12

    Using user input to determine how many times a for loop iterates

  13. 13

    How to find all the groups the user is a member? (LDAP)

  14. 14

    How to prevent user to go to login page after logged in in Vuejs?

  15. 15

    How to select all data from table if user login correct flutter

  16. 16

    How do I redirect a user after login with fetch()

  17. 17

    how to call a plugin after user login in joomla 3.0

  18. 18

    How to check if a user of my web application is trying to login with different credentials?

  19. 19

    How The Spring Security is redirecting to Login page if User is unauthorized

  20. 20

    how to define a function that counts how many times the words from 'the LM word' list (all words in total) appear in the text?

  21. 21

    Laravel login as another user

  22. 22

    How can I check a user from wp_user table on a custom login page?

  23. 23

    How do I calculate the total sum of my cart to my dictionary list based on user input menu?

  24. 24

    How does one write an SQL query to add up a total of balances for each user's most recent transaction?

  25. 25

    How do i find the total number of subjectsthat has no prerequisites using agregation?

  26. 26

    Websphere MQ- How to find the total number of messages that pass througha queue manager

  27. 27

    Find how many times a table must be searched to get a match Oracle database

  28. 28

    How to find UNIX times corresponding to a particular date in a very large file in R

  29. 29

    How can I find out the number of times an RSK transaction has been confirmed on the RSK blockchain?

ホットタグ

アーカイブ