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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:35:40+00:00 2026-05-26T11:35:40+00:00

I want to calculate a value by interpolating the value between two nearest neighbours.

  • 0

I want to calculate a value by interpolating the value between two nearest neighbours.
I have a subquery that returns the values of the neighbours and their relative distance, in the form of two columns with two elements.

Let’s say:

(select ... as value, ... as distance 
 from [get some neighbours by distance] limit 2) as sub

How can I calculate the value of the point by linear interpolation? Is it possible to do that in a single query?

Example: My point has the neighbour A with value 10 at distance 1, and the neighbour B with value 20 at distance 4. The function should return a value 10 * 4 + 20 * 1 / 5 = 12 for my point.

I tried the obvious approach

select sum(value * (sum(distance)-distance)) / sum(distance)

which will fail because you cannot work with group clauses inside group clauses. Using another subquery returning the sum is not possible either, because then I cannot forward the individual values at the same time.

  • 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-26T11:35:40+00:00Added an answer on May 26, 2026 at 11:35 am

    This is an ugly hack (based on a abused CTE ;). The crux of it is that

    value1 * distance2 + value2 * distance1
    

    Can, by dividing by distance1*distance2, be rewritten to

    value1/distance1 + value2/distance2
    

    So, the products (or divisions) can stay inside their rows. After the summation, multiplying by (distance1*distance2) rescales the result to the desired output. Generalisation to more than two neighbors is left as an exercise to the reader.YMMV

    DROP TABLE tmp.points;
    CREATE TABLE tmp.points
        ( pname VARCHAR NOT NULL PRIMARY KEY
        , distance INTEGER NOT NULL
        , value INTEGER
        );
    
    INSERT INTO tmp.points(pname, distance, value) VALUES
      ( 'A' , 1, 10 )
    ,  ( 'B' , 4, 20 )
    ,  ( 'C' , 10 , 1)
    ,  ( 'D' , 11 , 2)
      ;
    WITH RECURSIVE twin AS (
        select 1::INTEGER AS zrank
        , p0.pname AS zname
        , p0.distance AS dist
        , p0.value AS val
        , p0.distance* p0.value AS prod
        , p0.value::float / p0.distance AS frac
        FROM tmp.points p0
        WHERE NOT EXISTS ( SELECT * FROM tmp.points px
            WHERE px.distance < p0.distance)
        UNION
        select 1+twin.zrank AS zrank
        , p1.pname AS zname
        , p1.distance AS dist
        , p1.value AS val
        , p1.distance* p1.value AS prod
        , p1.value::float / p1.distance AS frac
        FROM tmp.points p1, twin
        WHERE p1.distance > twin.dist
        AND NOT EXISTS ( SELECT * FROM tmp.points px
            WHERE px.distance > twin.dist
            AND px.distance < p1.distance
            )   
        )
    -- SELECT * from twin ;
    SELECT min(zname) AS name1, max(zname) AS name2
        , MIN(dist) * max(dist) *SUM(frac) / SUM(dist) AS score
        FROM twin
        WHERE zrank <=2
        ;
    

    The result:

    CREATE TABLE
    INSERT 0 4
     name1 | name2 | score 
    -------+-------+-------
     A     | B     |    12
    

    Update: this one is a bit cleaner … ties are still not handled (need a window function or a LIMIT 1 clause in the outer query for that)

    WITH RECURSIVE twin AS (
        select 1::INTEGER AS zrank
        , p0.pname AS name1
        , p0.pname AS name2
        , p0.distance AS dist
        FROM tmp.points p0
        WHERE NOT EXISTS ( SELECT * FROM tmp.points px
            WHERE px.distance < p0.distance)
        UNION
        select 1+twin.zrank AS zrank
        , twin.name1 AS name1
        , p1.pname AS name2
        , p1.distance AS dist
        FROM tmp.points p1, twin
        WHERE p1.distance > twin.dist
        AND NOT EXISTS ( SELECT * FROM tmp.points px
            WHERE px.distance > twin.dist
            AND px.distance < p1.distance
            )
        )
    SELECT twin.name1, twin.name2
        , (p1.distance * p2.value + p2.distance * p1.value) / (p1.distance+p2.distance)  AS score
        FROM twin
        JOIN tmp.points p1 ON (p1.pname = twin.name1)
        JOIN tmp.points p2 ON (p2.pname = twin.name2)
        WHERE twin.zrank =2
        ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to calculate the angle between two vectors a and b. Lets assume
I want to calculate the difference between two times, one of which is the
Specifically: I have two unsigned integers (a,b) and I want to calculate (a*b)%UINT_MAX (UINT_MAX
I have a dropdownlist in ASP.NET Webforms that I want to calculate how many
I want to calculate the distance between the initial value of the touchstart and
I have an image and I want to calculate the average gray value of
I have a table and want to calculate each element like: calc-this-cost * calc-this-cost(value
I want to calculate the difference of days between two dates. My code works
I want to calculate the time span between 2 times which I saved in
I have a coordinate from Core Location and want to calculate the co-ordinate given

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.