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

  • Home
  • SEARCH
  • 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 56947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:36:52+00:00 2026-05-10T17:36:52+00:00

I want to record user states and then be able to report historically based

  • 0

I want to record user states and then be able to report historically based on the record of changes we’ve kept. I’m trying to do this in SQL (using PostgreSQL) and I have a proposed structure for recording user changes like the following.

CREATE TABLE users (   userid SERIAL NOT NULL PRIMARY KEY,    name VARCHAR(40),    status CHAR NOT NULL );  CREATE TABLE status_log (   logid SERIAL,    userid INTEGER NOT NULL REFERENCES users(userid),    status CHAR NOT NULL,    logcreated TIMESTAMP ); 

That’s my proposed table structure, based on the data.

For the status field ‘a’ represents an active user and ‘s’ represents a suspended user,

INSERT INTO status_log (userid, status, logcreated) VALUES (1, 's', '2008-01-01');  INSERT INTO status_log (userid, status, logcreated) VALUES (1, 'a', '2008-02-01');  

So this user was suspended on 1st Jan and active again on 1st of February.

If I wanted to get a suspended list of customers on 15th January 2008, then userid 1 should show up. If I get a suspended list of customers on 15th February 2008, then userid 1 should not show up.

1) Is this the best way to structure this data for this kind of query?

2) How do I query the data in either this structure or in your proposed modified structure so that I can simply have a date (say 15th January) and find a list of customers that had an active status on that date in SQL only? Is this a job for SQL?

  • 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. 2026-05-10T17:36:52+00:00Added an answer on May 10, 2026 at 5:36 pm

    This can be done, but would be a lot more efficient if you stored the end date of each log. With your model you have to do something like:

    select l1.userid from status_log l1 where l1.status='s' and l1.logcreated = (select max(l2.logcreated)                      from status_log l2                      where l2.userid = l1.userid                      and   l2.logcreated <= date '2008-02-15'                     ); 

    With the additional column it woud be more like:

    select userid from status_log where status='s' and logcreated <= date '2008-02-15' and logsuperseded >= date '2008-02-15'; 

    (Apologies for any syntax errors, I don’t know Postgresql.)

    To address some further issues raised by Phil:

    A user might get moved from active, to suspended, to cancelled, to active again. This is a simplified version, in reality, there are even more states and people can be moved directly from one state to another.

    This would appear in the table like this:

    userid  from       to         status FRED    2008-01-01 2008-01-31 s FRED    2008-02-01 2008-02-07 c FRED    2008-02-08            a 

    I used a null for the ‘to’ date of the current record. I could have used a future date like 2999-12-31 but null is preferable in some ways.

    Additionally, there would be no ‘end date’ for the current status either, so I think this slightly breaks your query?

    Yes, my query would have to be re-written as

    select userid from status_log where status='s' and logcreated <= date '2008-02-15' and (logsuperseded is null or logsuperseded >= date '2008-02-15'); 

    A downside of this design is that whenever the user’s status changes you have to end date their current status_log as well as create a new one. However, that isn’t difficult, and I think the query advantage probably outweighs this.

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

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • Best Answers 0
  • User 1

Related Questions

No related questions found

  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer No, there isn't a way to specify a callback function/macro,… May 11, 2026 at 8:54 am
  • added an answer Try the JSON based serialization package in ascorelib. [...]but I'm… May 11, 2026 at 8:54 am
  • added an answer In order to fire the HttpModule you have to have… May 11, 2026 at 8:54 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.