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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:21:36+00:00 2026-06-12T17:21:36+00:00

To compute the average angle in a table (angles in degrees [0, 360] )

  • 0

To compute the average angle in a table (angles in degrees [0, 360]) I use the following statement:

SELECT 
    (CASE WHEN (a < 0.0) 
    THEN a + 360.0 
    ELSE a END) as angle
FROM (
    SELECT 
        degrees(atan2(avg(sin(radians(x))), avg(cos(radians(x))))) as a
    FROM
        angle_t
    ) as t
UNION
SELECT
    x
FROM
    angle_t

when it came to testing I tried my table containing yahoo weather data:

WITH angle_t(x) AS (
    SELECT 
        cast(wind_direction as double precision)
    FROM
        weather_yahoo
    WHERE 
        time >= current_date - interval '1 days' - interval '1 hours'
    AND 
        time <= current_date - interval '1 days')

The output was:

246.670436944698
250.0
240.0

I wondered why the average angle wasn’t 245 but 246.67… so I ran another test with apparantly equal input data:

WITH angle_t(x) AS (VALUES 
    (240 :: double precision),
    (250))

The output showed the (un-)expected result:

245.0
250.0
240.0

Can anyone explain this to me? (this is PostgreSQL 8.4)

  • 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-12T17:21:37+00:00Added an answer on June 12, 2026 at 5:21 pm

    The UNION operator eliminates duplicate entries.

    From the documentation [emphasis mine]:

    UNION effectively appends the result of query2 to the result of query1 (although there is no guarantee that this is the order in which the rows are actually returned). Furthermore, it eliminates duplicate rows from its result, in the same way as DISTINCT, unless UNION ALL is used.

    Therefore, if UNION ALL is used, the explanation for the unexpected outcome is obvious:

    246.670436944698
    250.0
    240.0
    250.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Compute the average distance from a node to the root in a worst-case tree
How do you compute the average height of a binary search tree when adding
I'm trying to compute the average of a field over various subsets of a
After Compute Capability 2.0 (Fermi) was released, I've wondered if there are any use
Is there any minimum compute capability to use CUDA-OpenGL interoperability? I did not find
I am trying to compute an average value from a collection using the mongodb
I have a table called Sessions with two datetime columns: start and end. For
I want to calculate the average of a set of angles, which represents source
I ran into unexpected behavior with a function I wrote to compute the average
Using pandas I can compute simple moving average SMA using pandas.stats.moments.rolling_mean exponential moving average

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.