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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:03:24+00:00 2026-05-11T00:03:24+00:00

I have a table named visiting that looks like this: id | visitor_id |

  • 0

I have a table named visiting that looks like this:

id | visitor_id | visit_time  -------------------------------------  1 |          1 | 2009-01-06 08:45:02   2 |          1 | 2009-01-06 08:58:11  3 |          1 | 2009-01-06 09:08:23   4 |          1 | 2009-01-06 21:55:23  5 |          1 | 2009-01-06 22:03:35 

I want to work out a sql that can get how many times a user visits within one session(successive visit’s interval less than 1 hour).

So, for the example data, I want to get following result:

visitor_id | count -------------------          1 |     3          1 |     2 

BTW, I use postgresql 8.3. Thanks!

UPDATE: updated the timestamps in the example data table. sorry for the confusion.
UPDATE: I don’t care much if the solution is a single sql query, using store procedure, subquery etc. I only care how to get it done 🙂

  • 1 1 Answer
  • 1 View
  • 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-11T00:03:24+00:00Added an answer on May 11, 2026 at 12:03 am

    The question is slightly ambiguous because you’re making the assumption or requiring that the hours are going to start at a set point, i.e. a natural query would also indicate that there’s a result record of (1,2) for all the visits between the hour of 08:58 and 09:58. You would have to ‘tell’ your query that the start times are for some determinable reason visits 1 and 4, or you’d get the natural result set:

    visitor_id | count  --------------------          1 | 3          1 | 2 <- extra result starting at visit 2          1 | 1 <- extra result starting at visit 3          1 | 2          1 | 1 <- extra result starting at visit 5 

    That extra logic is going to be expensive and too complicated for my fragile mind this morning, somebody better than me at postgres can probably solve this.

    I would normally want to solve this by having a sessionkey column in the table I could cheaply group by for perforamnce reasons, but there’s also a logical problem I think. Deriving session info from timings seems dangerous to me because I don’t believe that the user will be definitely logged out after an hours activity. Most session systems work by expiring the session after a period of inactivity, i.e. it’s very likely that a visit after 9:45 is going to be in the same session because your hourly period is going to be reset at 9:08.

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

Sidebar

Related Questions

I have a table named Info of this schema: int objectId; int time; int
If I have a table field named 'description', what would be the SQL (using
If I have a table like this: CREATE TABLE sizes ( name ENUM('small', 'medium',
I have table named 'testing' of 3 columns i.e. user,score,time in that 'time' is
I have a table named buildings that contains a varchar(50) field named use. The
I have a table named categories, which contains ID(long), Name(varchar(50)), parentID(long), and shownByDefault(boolean) columns.
I have a database table named call with columns call_time, location, emergency_type and there
I have a database table (named Topics) which includes these fields : topicId name
I have a table in my Postgres database with columns named type, desc, and
I have table with 50 entries (users with such details like Name Surname Location

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.