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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:51:54+00:00 2026-06-12T02:51:54+00:00

I have a a PostgreSQL database table with the following simplified structure: Device Id

  • 0

I have a a PostgreSQL database table with the following simplified structure:

  • Device Id varchar
  • Pos_X (int)
  • Pos_Y (int)

Basically this table contains a lot of two dimensional waypoint data for devices. Now I want to design a query which reduces the number of coordinates in the output. It should aggregate nearby coordinates (for a certain x,y threshold)
An example:

row 1: DEVICE1;603;1205

row 2: DEVICE1;604;1204

If the threshold is 5, these two rows should be aggregated since the variance is smaller than 5.
Any idea how to do this in PostgreSQL or SQL in general?

  • 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-12T02:51:56+00:00Added an answer on June 12, 2026 at 2:51 am

    Use the often overlooked built-in function width_bucket() in combination with your aggregation:

    If your coordinates run from, say, 0 to 2000 and you want to consolidate everything within squares of 5 to single points, I would lay out a grid of 10 (5*2) like this:

    SELECT device_id
         , width_bucket(pos_x, 0, 2000, 2000/10) * 10 AS pos_x
         , width_bucket(pos_y, 0, 2000, 2000/10) * 10 AS pos_y
         , count(*) AS ct -- or any other aggregate
    FROM   tbl
    GROUP  BY 1,2,3
    ORDER  BY 1,2,3;
    

    To minimize the error you could GROUP BY the grid as demonstrated, but save actual average coordinates:

    SELECT device_id
         , avg(pos_x)::int AS pos_x   -- save actual averages to minimize error
         , avg(pos_y)::int AS pos_y   -- cast if you need to
         , count(*)        AS ct      -- or any other aggregate
    FROM   tbl
    GROUP  BY
           device_id
         , width_bucket(pos_x, 0, 2000, 2000/10) * 10  -- aggregate by grid
         , width_bucket(pos_y, 0, 2000, 2000/10) * 10
    ORDER  BY 1,2,3;
    

    sqlfiddle demonstrating both alongside.

    Well, this particular case could be simpler:

    ...
    GROUP  BY
           device_id
         , (pos_x / 10) * 10          -- truncates last digit of an integer
         , (pos_y / 10) * 10
    ...
    

    But that’s just because the demo grid size of 10 conveniently matches the decimal system. Try the same with a grid size of 17 or something …


    Expand to timestamps

    You can expand this approach to cover date and timestamp values by converting them to unix epoch (number of seconds since ‘1970-1-1’) with extract().

    SELECT extract(epoch FROM '2012-10-01 21:06:38+02'::timestamptz);
    

    When you are done, convert the result back to timestamp with time zone:

    SELECT timestamptz 'epoch' + 1349118398 * interval '1s';
    

    Or simply to_timestamp():

    SELECT to_timestamp(1349118398);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table and sequence in my postgresql-8.4 database: CREATE TABLE complexobjectpy
I have the following table in my postgreSQL 8.3.14 database timestamp status 2012-03-12 19:15:01
I have a table in my postgresql 8.4 database like this: id(serial), event_type_id(id, foreign
I have a PostGreSQL database with the following data model: CREATE TABLE staff (s_id
i have latitude and longitude columns in location table in PostgreSQL database, and I
I have a table in a postgresql-9.1.x database which is defined as follows: #
I have the following database table with information about people, diseases, and drugs: PERSON_T
I have a PostgreSql 9.04 database that contains a postgres implementation of the ASPNet
I have executed the following create statement using SQLWorkbench at my target postgresql database:
Suppose we have a PostgreSQL database with two tables A, B. table A columns:

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.