Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6952447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:21:02+00:00 2026-05-27T14:21:02+00:00

I have a table that contains computer login and logoff events. Each row is

  • 0

I have a table that contains computer login and logoff events. Each row is a separate event with a timestamp, machine name, login or logoff event code and other details. I need to create a SQL procedure that goes through this table and locates corresponding login and logoff event and insert new rows into another table that contain the machine name, login time, logout time and duration time.

So, should I use a cursor to do this or is there a better way to go about this? The database is pretty huge so efficiency is certainly a concern. Any suggested pseudo code would be great as well.

[edit : pulled from comment]

Source table:

History (
      mc_id
    , hs_opcode
    , hs_time
)

Existing data interpretation:

Login_Event  = unique mc_id, hs_opcode = 1, and hs_time is the timestamp
Logout_Event = unique mc_id, hs_opcode = 2, and hs_time is the timestamp
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:21:03+00:00Added an answer on May 27, 2026 at 2:21 pm

    First, your query will be simpler (and faster) if you can order the data in such a way that you don’t need a complex subquery to pair up the rows. Since MySQL doesn’t support CTE to do this on-the-fly, you’ll need to create a temporary table:

    CREATE TABLE history_ordered (
      seq INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
      hs_id INT,
      mc_id VARCHAR(255),
      mc_loggedinuser VARCHAR(255),
      hs_time DATETIME,
      hs_opcode INT
    );
    

    Then, pull and sort from your original table into the new table:

    INSERT INTO history_ordered (
      hs_id, mc_id, mc_loggedinuser,
      hs_time, hs_opcode)
    SELECT
      hs_id, mc_id, mc_loggedinuser,
      hs_time, hs_opcode
    FROM history ORDER BY mc_id, hs_time;
    

    You can now use this query to correlate the data:

    SELECT li.mc_id,
           li.mc_loggedinuser,
           li.hs_time as login_time,
           lo.hs_time as logout_time
    FROM   history_ordered AS li
    JOIN   history_ordered AS lo
      ON   lo.seq = li.seq + 1
       AND li.hs_opcode = 1;
    

    For future inserts, you can use a trigger like below to keep your duration table updated automatically:

    DELIMITER $$
    CREATE TRIGGER `match_login` AFTER INSERT ON `history`
    FOR EACH ROW
    BEGIN
     IF NEW.hs_opcode = 2 THEN
      DECLARE _user VARCHAR(255);
      DECLARE _login DATETIME;
      SELECT mc_loggedinuser, hs_time FROM history
      WHERE hs_time = (
       SELECT MAX(hs_time) FROM history
       WHERE hs_opcode = 1
       AND mc_id = NEW.mc_id
      ) INTO _user, _login;
      INSERT INTO login_duration
      SET machine = NEW.mc_id,
      logout = NEW.hs_time,
      user = _user,
      login = _login;
     END IF;
    END$$
    DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains in each row an input and a save
I have a table that contains the next columns: ip(varchar 255), index(bigint 20), time(timestamp)
I have a table that contains thousands of URLs. Each URL will have a
I have a table that contains a column of XML Datatype (column name FileContent).
I have a table that contains my node data in a tree view. Each
I have a table that contains URL strings, i.e. /A/B/C /C/E /C/B/A/R Each string
I have a table that contains tasks and I want to give these an
I have a table that contains maybe 10k to 100k rows and I need
I have a table that contains some blob fields that I don't want to
I have a table that contains intervals: CREATE TABLE tbl ( user_id: INTEGER, start:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.