Re: Fwd: PSQL Help from your biggest fan

From: Raghavendra <raghavendra(dot)rao(at)enterprisedb(dot)com>
To: Evan Stanford <evanstanford1(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>
Subject: Re: Fwd: PSQL Help from your biggest fan
Date: 2012-08-18 05:24:24
Message-ID: CA+h6AhhaT4gQLdZ+1UMNt542_QmiKMr4_pxJV+H4tQ+DbnUZ_A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>
>
> I tried your code in Postgres 8.2:
>

8.2 ?, Seems you have tested it in very Old version.

> CREATE OR REPLACE FUNCTION _final_mode(anyarray)
>
> RETURNS anyelement AS
>
> $BODY$
>
> SELECT a
>
> FROM unnest($1) a
>
> GROUP BY 1
>
> ORDER BY COUNT(1) DESC, 1
>
> LIMIT 1;
>
> $BODY$
>
> LANGUAGE 'sql' IMMUTABLE;
>
> DROP AGGREGATE IF EXISTS mode(anyelement);
>
> CREATE AGGREGATE mode(anyelement)
> (SFUNC=array_append,STYPE=anyarray,FINALFUNC=_final_mode,INITCOND='{}');
>
>
> I also added the unnest function (although mine seemed to already have it).
>
>
>
>
> I tested it like this:
>
> sdap=# create table Z as (select 7 as value);
>
> sdap=# select mode(value) from Z;
>
> mode
>
> ------
>
> 7
>
> (1 row) --WORKS
>
>
>
> sdap=# insert into Z values (8);
>
> sdap=# insert into Z values (8);
>
> select mode(value) from Z;
>
> mode
>
> ------
>
> 8
>
> (1 row) --WORKS
>
>
>
> sdap=# insert into Z values (NULL);
>
> sdap=# select mode(value) from Z;
>
> ERROR: null array element where not supported (arrayfuncs.c:872)
>
>
>
> Any ideas?
>
>
>
It works very well in the latest version. Have you checked it.

bash-4.1$ psql
psql.bin (9.1.4)
Type "help" for help.

postgres=# \pset null NULL
Null display is "NULL".
postgres=# select * from z;
value
-------
7
NULL
(2 rows)

postgres=# select mode(value) from Z;
mode
------
7
(1 row)

--Raghav

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jasen Betts 2012-08-18 07:12:20 Re: Alternatives to very large tables with many performance-killing indicies?
Previous Message Chris Travers 2012-08-18 02:40:58 Re: Views versus user-defined functions: formatting, comments, performance, etc.