473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

aggregate generic ANYARRAY

Thought others might find this useful (PostgreSQL 7.4+ only)

When used, it outputs an array of the inputs, in order.

CREATE OR REPLACE FUNCTION aggregate_array(ANYARRAY,ANYELEMENT) RETURNS
ANYARRAY AS '
SELECT
CASE
WHEN $1 IS NULL
THEN ARRAY[$2]
WHEN $2 IS NULL
THEN $1
ELSE array_append($1,$2)
END;
' LANGUAGE 'SQL';

CREATE AGGREGATE aggarray (BASETYPE = ANYELEMENT, SFUNC = aggregate_array,
STYPE = ANYARRAY);

-- *********************************************
--simple demonstration of aggarray
-- *********************************************
SELECT
n.nspname,
aggarray(relname)
FROM pg_catalog.pg_class c
INNER JOIN pg_catalog.pg_namespace n ON (c.relnamespace = n.oid)
WHERE c.relkind = 'r'
GROUP BY c.relnamespace,n.nspname;

/*
-- results from test agg array
nspname | aggarray
----------------------------------------------------------------------
information_schema |
{sql_sizing,sql_sizing_profiles,sql_features,sql_i mplementation_info,sql_lan
guages,sql_packages}
pg_catalog |
{pg_shadow,pg_namespace,pg_conversion,pg_depend,pg _attrdef,pg_constraint,pg_
database,pg_description,pg_group,pg_proc,pg_rewrit e,pg_statistic,pg_type,pg_
attribute,pg_class,pg_inherits,pg_index,pg_operato r,pg_opclass,pg_am,pg_amop
,pg_amproc,pg_language,pg_largeobject,pg_aggregate ,pg_trigger,pg_listener,pg
_cast}
(2 rows)

Time: 10.000 ms
test=#

*/

--
Tom Hebbron
www.hebbron.com
+39 0444540626 (Vicenza, Italy)


Nov 12 '05 #1
1 1809
Tom Hebbron wrote:
When used, it outputs an array of the inputs, in order. [...snip...] CREATE AGGREGATE aggarray (BASETYPE = ANYELEMENT, SFUNC = aggregate_array,
STYPE = ANYARRAY);
There is really no need for the aggregate_array() function. See the
example in the docs:
http://www.postgresql.org/docs/curre...tic/xaggr.html

So it would look like this:
CREATE AGGREGATE aggarray (basetype = anyelement, sfunc = array_append,
stype = anyarray, initcond = '{}' );
-- *********************************************
--simple demonstration of aggarray
-- *********************************************

regression=# SELECT n.nspname, aggarray(relname) FROM
pg_catalog.pg_class c INNER JOIN pg_catalog.pg_namespace n ON
(c.relnamespace = n.oid) WHERE c.relkind = 'r' GROUP BY
c.relnamespace,n.nspname;
nspname |

aggarray

--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
information_schema |
{sql_features,sql_implementation_info,sql_language s,sql_packages,sql_sizing,sql_sizing_profiles}
MySchema | {Foo}
pg_catalog |
{pg_shadow,pg_constraint,pg_database,pg_descriptio n,pg_group,pg_proc,pg_rewrite,pg_statistic,pg_type ,pg_attribute,pg_class,pg_inherits,pg_index,pg_ope rator,pg_opclass,pg_am,pg_amop,pg_amproc,pg_langua ge,pg_largeobject,pg_aggregate,pg_trigger,pg_liste ner,pg_cast,pg_namespace,pg_conversion,pg_depend,p g_attrdef}
public | {foo,mytable,fw_chain}
(4 rows)

Time: 2.518 ms

HTH,

Joe
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: pb648174 | last post by:
In the below structure, if I wanted to get the Id of the comment for each Generic record having the latest comment time, how would I do that not using a subquery? Table: Generic Id Description...
2
by: Greg Stark | last post by:
I find I often want to be able to do joins against views where the view are aggregates on a column that has an index. Ie, something like SELECT a.*, v.n FROM a JOIN (select a_id,count(*) as n...
10
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to...
3
by: MrNobody | last post by:
I've read that the expression property for DataColumns is used to "Sets or retrieves the expresssion used to filter rows, calculate the values in a column, or create an aggregate column.". I...
1
by: Najib Abi Fadel | last post by:
Hi i have an ordered table of dates let's say: 1/1/2004 8/1/2004 15/1/2004 29/1/2004 5/2/2004 12/2/2004
3
by: Raghu | last post by:
Hello all, Can somebody help me hopw to resolve teh probelm of aggregate initialisation in c++. Her eis the piece of code. #include<stdio.h> class MyTest { public:
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
2
by: btakacs | last post by:
Hi Could anybody help me to create some kind of Generic Aggregate concatenation in db2? I have tables that holds this kind of data: "dog" "big" "dog" "strong" "cat" "quick" "dog" "big"
4
by: shapper | last post by:
Hello, I have the following Linq query: var q = (from p in database.Posts join pt in database.PostsTags on p.PostID equals pt.PostID join t in database.Tags on pt.TagID equals t.TagID group...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.