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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:58:36+00:00 2026-05-27T06:58:36+00:00

I am trying to understand aggregate functions and I need help. So for instance

  • 0

I am trying to understand aggregate functions and I need help.

So for instance the following sample:

CREATE OR REPLACE FUNCTION array_median(timestamp[])
  RETURNS timestamp AS
$$
    SELECT CASE WHEN array_upper($1,1) = 0 THEN null ELSE asorted[ceiling(array_upper(asorted,1)/2.0)] END
    FROM (SELECT ARRAY(SELECT ($1)[n] FROM
generate_series(1, array_upper($1, 1)) AS n
    WHERE ($1)[n] IS NOT NULL
            ORDER BY ($1)[n]
) As asorted) As foo ;
$$
  LANGUAGE 'sql' IMMUTABLE;


CREATE AGGREGATE median(timestamp) (
  SFUNC=array_append,
  STYPE=timestamp[],
  FINALFUNC=array_median
)

I am not understanding the structure/logic that needs to go into the select statement in the aggregate function itself. Can someone explain what the flow/logic is?

I am writing an aggregate, a strange one, that the return is always the first string it ever sees.

  • 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-27T06:58:36+00:00Added an answer on May 27, 2026 at 6:58 am

    You’re showing a median calculation, but want the first text value you see?

    Below is how to do that. Assuming you want the first non-null value, that is. If not, you’ll need to keep track of if you’ve got a value already or not.

    The accumulator function is written as plpgsql and sql – the plpgsql one lets you use variable names and debug it too. It simply uses COALESCE against the previous accumulated value and the new value and returns the first non-null. So – as soon as you have a non-null in the accumulator everything else gets ignored.

    You may also want to consider the “first_value” window function for this sort of thing if you’re on a modern (8.4+) version of PostgreSQL.

    http://www.postgresql.org/docs/9.1/static/functions-window.html

    HTH

    BEGIN;
    
    CREATE FUNCTION remember_first(acc text, newval text) RETURNS text AS $$
    BEGIN
        RAISE NOTICE '% vs % = %', acc, newval, COALESCE(acc, newval);
        RETURN COALESCE(acc, newval);
    END;
    $$ LANGUAGE plpgsql IMMUTABLE;
    
    CREATE FUNCTION remember_first_sql(text,text) RETURNS text AS $$
        SELECT COALESCE($1, $2);
    $$ LANGUAGE SQL IMMUTABLE;
    
    -- No "initcond" means we start out with null
    --      
    CREATE AGGREGATE first(text) (
        sfunc = remember_first,
        stype = text
    );
    
    CREATE TEMP TABLE tt (t text);
    INSERT INTO tt VALUES ('abc'),('def'),('ghi');
    
    SELECT first(t) FROM tt;
    
    ROLLBACK;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand what Sql Profiler means by emitting sp_reset_connection. I have the following,
Trying to understand why this doesn't work. I keep getting the following errors: left
Trying to understand resources in java-land. I believe the following is true: Resources loaded
Trying to understand, why my C++/Qt application creates 18 threads, while i don't create
Trying to understand this MSDN sample but I'm confused about these lines: IAsyncResult result
I am trying understand why the .wrap() function in my basic table of contents
Trying to understand an fft (Fast Fourier Transform) routine I'm using (stealing)(recycling) Input is
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand something. I created a d:\svn\repository on my server. I committed folders

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.