473,657 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,ANYEL EMENT) 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(relnam e)
FROM pg_catalog.pg_c lass c
INNER JOIN pg_catalog.pg_n amespace n ON (c.relnamespace = n.oid)
WHERE c.relkind = 'r'
GROUP BY c.relnamespace, n.nspname;

/*
-- results from test agg array
nspname | aggarray
----------------------------------------------------------------------
information_sch ema |
{sql_sizing,sql _sizing_profile s,sql_features, sql_implementat ion_info,sql_la n
guages,sql_pack ages}
pg_catalog |
{pg_shadow,pg_n amespace,pg_con version,pg_depe nd,pg_attrdef,p g_constraint,pg _
database,pg_des cription,pg_gro up,pg_proc,pg_r ewrite,pg_stati stic,pg_type,pg _
attribute,pg_cl ass,pg_inherits ,pg_index,pg_op erator,pg_opcla ss,pg_am,pg_amo p
,pg_amproc,pg_l anguage,pg_larg eobject,pg_aggr egate,pg_trigge r,pg_listener,p g
_cast}
(2 rows)

Time: 10.000 ms
test=#

*/

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


Nov 12 '05 #1
1 1830
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(relnam e) FROM
pg_catalog.pg_c lass c INNER JOIN pg_catalog.pg_n amespace n ON
(c.relnamespace = n.oid) WHERE c.relkind = 'r' GROUP BY
c.relnamespace, n.nspname;
nspname |

aggarray

--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
information_sch ema |
{sql_features,s ql_implementati on_info,sql_lan guages,sql_pack ages,sql_sizing ,sql_sizing_pro files}
MySchema | {Foo}
pg_catalog |
{pg_shadow,pg_c onstraint,pg_da tabase,pg_descr iption,pg_group ,pg_proc,pg_rew rite,pg_statist ic,pg_type,pg_a ttribute,pg_cla ss,pg_inherits, pg_index,pg_ope rator,pg_opclas s,pg_am,pg_amop ,pg_amproc,pg_l anguage,pg_larg eobject,pg_aggr egate,pg_trigge r,pg_listener,p g_cast,pg_names pace,pg_convers ion,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
1252
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 Table: Comment Id
2
3662
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 group by a_id) as v USING (a_id) where there's an index on b.a_id. assume there are other tables being joined so I can't just move the aggregate to the outermost layer.
10
11925
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 post) *I'm running a total query, of the form
3
14307
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 have seen examples on how to filter a column, but how would I filter out an entire row depending on the value of a column? For example, if I wanted to filter out every row in a multi-column table where the "Is Fubar" column equals "true", how would...
1
2718
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
3339
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
6108
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 pointers, but I'm not sure. Please help. The code: void equate(matrix *A, matrix *B) { int i, j; assert(A.row_dim == B.col_dim && A.col_dim == B.col_dim); for(i=0; i < A.row_dim; i++) for(j=0; j < A.col_dim; j++)
2
2061
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
4717
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 t by p into pt select new PostPaper {
0
8397
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7333
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.