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

  • Home
  • SEARCH
  • 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 8141007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:21:09+00:00 2026-06-06T12:21:09+00:00

My Postgres query calculates statistical aggregate from a bunch of sensor readings: SELECT to_char(ipstimestamp,

  • 0

My Postgres query calculates statistical aggregate from a bunch of sensor readings:

SELECT to_char(ipstimestamp, 'YYYYMMDDHH24') As row_name, 
to_char(ipstimestamp, 'FMDD mon FMHH24h') As hour_row_name, 
varid As category, 

(AVG(ipsvalue)::NUMERIC(5,2)) ||', ' || 
(MAX(ipsvalue)::NUMERIC(5,2))::TEXT  ||', ' || 
(MIN(ipsvalue)::NUMERIC(5,2))::TEXT  ||', ' || 
(STDDEV(ipsvalue)::NUMERIC(5,2))::TEXT  ||', ' As StatisticsValue 

FROM loggingdb_ips_integer As log 
JOIN ipsobjects_with_parent ips ON log.varid = ips.objectid 
AND (ipstimestamp > (now()- '2 days'::interval)) 
GROUP BY row_name, hour_row_name, category;

This works fine as long as I have >1 ipsvalue/hour. If the hourly COUNT(ipsvalue)<2, however, StatisticsValue returns NULL without any Postgres errors.

If I comment out STTDEV, as in the following:

(AVG(ipsvalue)::NUMERIC(5,2)) ||', ' || 
(MAX(ipsvalue)::NUMERIC(5,2))::TEXT  ||', ' || 
(MIN(ipsvalue)::NUMERIC(5,2))::TEXT  ||', ' As value 

then all three stats are calculated correctly. I therefore conclude that an illegittimate STDDEV brings down the whole query. I would rather have illegittimate STDDEVs returning 0. I tried to COALESCE the STDDEV line, to no avail. What can be done???

  • 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-06T12:21:11+00:00Added an answer on June 6, 2026 at 12:21 pm

    COALESCE should work.

    You could also use (it that fits you) the “population standard deviation” stddev_pop, instead of the “sample standard deviation” stddev_samp; the later is divides by n-1 and is aliased to STDDEV. stddev_pop, instead , divides by n , and it returns zero (instead of NULL) when given one sample.

    If you don’t know the difference between these estimators, it’s explained in every statistic textbook, eg http://en.wikipedia.org/wiki/Standard_deviation#Estimation

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

Sidebar

Related Questions

How can I do such query in Postgres? IF (select count(*) from orders) >
I want to remove duplicate rows return from a SELECT Query in Postgres I
I have a query in postgres insert into c_d (select * from cd where
I try this query from Oracle : select * from a_table@Postgres; Here is the
I have this postgres sql query: select * from stats where athlete_id = 5
I currently have the query running on Postgres: SELECT * FROM addenda.users WHERE users.username
The following Postgres SQL query will list all tables from all schemas and their
I've got a SQL query in Postgres that works fine from a SQL console/tool,
I have a problem with the query below in postgres SELECT u.username,l.description,l.ip,SUBSTRING(l.createdate,0,11) as createdate,l.action
This query: client.query({ text: SELECT * FROM users WHERE provider = ? AND remote_id

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.