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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:18:21+00:00 2026-05-20T23:18:21+00:00

I’m new to postgres, so please take it easy on me. I’m trying to

  • 0

I’m new to postgres, so please take it easy on me.

I’m trying to write a query so that for any user, I can pull ALL of the log files (both their activity and the activity of others) for one minute prior and one minute after their name appears in the logs within the same batchstamp.

chat.batchstamp is varchar
chat.datetime is timestamp
chat.msg is text
chat.action is text    (this is the field with the username)

Here are the separate commands I want to use, I just don’t know how to put them together and if this is really the right path to go on this.

SELECT batchstamp, datetime, msg FROM chat WHERE action LIKE 'username';

Anticipated output:

batchstamp   datetime      msg  
abc          2010-12-13 23:18:00 System logon          
abc          2010-12-13 10:12:13 System logon    
def          2010-12-14 11:12:18 System logon

SELECT * FROM chat WHERE datetime BETWEEN datetimefrompreviousquery - interval '1 minute' AND datetimefrompreviousquery + interval '1 minute';

Can you please help explain to me what I should do to feed data from the previous query in to the second query? I’ve looked at subqueries, but do I need to run two subqueries? Should I build a temporary table?

After this is all done, how do I make sure that the times the query matches are within the same batchstamp?

If you’re able to point me in the right direction, that’s great. If you’re able to provide the query, that’s even better. If my explanation doesn’t make sense, maybe I’ve been looking at this too long.

Thanks for your time.

Based on nate c’s code below, I used this:

SELECT * FROM chat, 
( SELECT batchstamp, datetime FROM chat WHERE action = 'fakeuser' ) 
AS log WHERE chat.datetime BETWEEN log.datetime - interval '1 minute' AND log.datetime + '1 minute';

It doesn’t seem to return every hit of ‘fakeuser’ and when it does, it pulls the logs from every ‘batchstamp’ instead of just the one where ‘fakeuser’ was found. Am I in for another nested query? What’s this type of procedure called so I can further research it?

Thanks again.

  • 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-20T23:18:22+00:00Added an answer on May 20, 2026 at 11:18 pm

    You first query can go in the from clause with ‘(‘ brackets around it and ‘as alias’ name. After that you can reference it as you would a normal table in the rest of the query.

    SELECT
    * 
    FROM chat,
    (
        SELECT
            batchstamp,
            datetime, 
            msg
        FROM log
        WHERE action LIKE 'username'
    ) AS log
    
    WHERE chat.datetime BETWEEN
        log.datetime - interval '1 minute'
        AND log.datetime + interval '1 minute';
    

    That should get you started.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want use html5's new tag to play a wav file (currently only supported
I've got a string that has curly quotes in it. I'd like to replace
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,

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.