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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:45:47+00:00 2026-06-14T06:45:47+00:00

I am trying to write a query to compare the number of average connections

  • 0

I am trying to write a query to compare the number of average connections today for a given computer to the number of average connections between 7 and 14 days ago. I figured this was best handled by a window function but I am not able to get the syntax correct for the date.

Assume I have a table of IP addresses and connection records called iptable with soucreip, destinationip, timestamp as the columns. Here is the query I am trying for the previous 7 day window to just get the count per sourceip:

select 
  sourceip, 
  destinationip, 
  timestamp, 
  count(*) OVER (PARTITION BY sourceip order by timestamp
                 RANGE BETWEEN now() - '7 day'::Interval PRECEDING
                               now() - '14 day'::Interval FOLLOWING)
from
 iptable;

What is the best way to write this type of query does the window function approach make sense or is there a more optimized way to do things for the case of large tables?

  • 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-14T06:45:48+00:00Added an answer on June 14, 2026 at 6:45 am

    Part of your issue is that you’ve chosen a terrible column name, "timestamp". timestamp is the name of a built-in data type, so to use it as a column name you must "double quote" it everywhere.

    That’s not all, though. Your window function syntax is wrong. See window function syntax. You forgot the AND; it’s RANGE BETWEEN .. PRECEDING AND ... FOLLOWING.

    Also, though it isn’t the cause of the issues, you should use the SQL-standard current_timestamp instead of now().

    That’ll get you to a new error:

    CREATE TABLE iptable ( sourceip cidr, destinationip cidr, "timestamp" timestamptz);
    
    regress=> select 
      sourceip, 
      destinationip, 
      timestamp, 
      count(*) OVER (PARTITION BY sourceip order by "timestamp" RANGE BETWEEN current_timestamp - '7 day'::Interval PRECEDING AND current_timestamp - '14 day'::Interval FOLLOWING)
    
    from
     iptable;
    ERROR:  RANGE PRECEDING is only supported with UNBOUNDED
    LINE 5: ... OVER (PARTITION BY sourceip order by "timestamp" RANGE BETW...
                                                                 ^
    

    which suggests that the current window function implementation won’t do what you want it to do. Sadly.

    The value PRECEDING and value FOLLOWING cases are currently only
    allowed in ROWS mode. They indicate that the frame starts or ends with
    the row that many rows before or after the current row. value must be
    an integer expression not containing any variables, aggregate
    functions, or window functions.

    Instead I’d just use a plain GROUP BY with a WHERE filter on the input rows.

    select 
      sourceip,
      count(sourceip) AS n_conns_7_to_14_days_ago
    from
     iptable
    WHERE age("timestamp") BETWEEN INTERVAL '7' DAY AND INTERVAL '14' DAY
    GROUP BY sourceip;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write Linq query on this Products table based on FacetTypes that
I am trying to write a query with some fancy joins. I know this
Im trying to write a query which will emit : the date/s which Overlapped
I'm trying to write a query that will return the closest match from a
I am trying to write a query and would like some help if possible.
I am trying to write a query post such that it returns a list
I'm trying to write a query to join a user table to an activity
I am trying to write the query for existing table data. TABLE_A and TABLE_B
I am trying to write a query where for each day I get the
I'm trying to write a query to pull some info from our database and

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.