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 9095133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:31:23+00:00 2026-06-16T23:31:23+00:00

I have a table for logging access data of a lab. The table struct

  • 0

I have a table for logging access data of a lab. The table struct like this:

create table accesslog
(
    userid int not null,
    direction int not null,
    accesstime datetime not null
);

This lab have only one gate that is under access control. So the users must first “enter” the lab before they can “leave”. In my original design, I set the “direction” field as a flag that is either 1 (for entering the lab) or -1 (for leaving the lab). So that I can use queries like:

SELECT SUM(direction) FROM accesslog;

to get the total user count within the lab. Theoretically, it worked; since the “direction” will always be in the patterns of 1 => -1 => 1 => -1 for any given userid.

But soon I found that the log message would lost in the transmission path from lab gate to server, being dropped either by busy network or by hardware glitches. Of course I can enforce the transmission path with sequence number, ACK, retransmission, hardware redundancy, etc., but in the end I might still get something like this:

userid   direction   accesstime
-------------------------------------
1         1          2013/01/03 08:30
1        -1          2013/01/03 09:20
1         1          2013/01/03 10:10
1        -1          2013/01/03 10:50
1        -1          2013/01/03 13:40
1         1          2013/01/03 18:00

It’s a recent log for user “1”. It’s clear that I’ve lost one log message for that user entering the lab between 10:50 to 13:40. While I query this data, he is still in the lab, so there is no exiting logs after 2013/01/03 18:00 yet; that’s affirmative.

My question is: is there any way to “find” this data inconsistence with SQL command ? There are total 5000 users within my system and the lab is operating 24 hour, there is no such “magic time” that the lab would be cleared. I’d be horrible if I’ve to write codes checking the continuity of “direction” field line-by-line, user-by-user.

I know it’s not possible to “fix” the log with correct data. I just want to know “Oh, I have a data inconsistency issue for userid=1” so that I can add an marked amending data to the correct the final statistic.

Any advice would be appreciated, even changing the table structure would be OK.

Thanks.

Edit: Sorry I didn’t mentioned the details.

Currently I’m using mixed SQL solution. The table showed above is MySQL, and it contains only logs within 24 hrs as the “real time” status for fast browsing.

Everyday at 03:00 AM a pre-scheduled process written in C++ on POSIX will be launched. This process will calculated the statistic data, and add the daily statistic to an Oracle DB, via a proprietary-protocol TCP socket, then it will remove the old data from MySQL.

The Oracle part is not handled by me and I can do nothing about it. I just want to make sure that the final statistics of each day is correct.

The data size is about 200,000 records per day — I know it’s sound crazy but it’s true.

  • 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-06-16T23:31:24+00:00Added an answer on June 16, 2026 at 11:31 pm

    Ok I figured it out. Thanks for the idea provided by a_horse_with_no_name.

    My final solution is this query:

    SELECT userid, COUNT(*), SUM(direction * rule) FROM (
        SELECT userid, direction, @inout := @inout * -1 AS rule
        FROM accesslog l, (SELECT @inout := -1) r
        ORDER by userid, accesstime
    ) g GROUP by userid;
    

    First I created a pattern with @inout that will yield 1 => -1 => 1 => -1 for each row in the “rule” column. Than I compare the direction field with rule column by calculating multiplication product.

    It’s OK even if there are odd records for certain users; since each user is supposed to follow identical or reversed pattern as “rule”. So the total sum of multiplication product should be equal to either COUNT() or -1 * COUNT().

    By checking SUM() and COUNT(), I can know exactly which userid had go wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a project where data is written to a logging table. Now, when
I use SQL Server 2005. I have a simple logging table that my web
I have an application that records activity in a table (Oracle 10g). The logging
I have table and this table contain result column with some entries. I just
I have table with 300 000 records (MyISAM). I get record from this table
I have table of data that is sorted as follows: Item | Sample |
I have below click_log table logging hits for some urls site ip ua direction
I have a data access layer which returns DataSets/DataTables by executing Stored Procedure. Everything
I'm searching for a high accessing logging solution. My table has this structure: |
I have a table containing the logging of a web app which tracks when

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.