aggregate-functions


Creating an aggregate function fails


While looking for an aggregate function that returns the nth element of a collection I found the following solution:
CREATE OR REPLACE FUNCTION _final_nth( anyarray, integer )
RETURNS anyelement AS
$BODY$
SELECT a
FROM unnest( $1 ) a
ORDER BY a
offset $2
LIMIT 1;
$BODY$
LANGUAGE 'sql' IMMUTABLE;
CREATE AGGREGATE nth( anyelement, integer ) (
SFUNC=array_append,
STYPE=anyarray,
FINALFUNC=_final_nth,
INITCOND='{}'
);
Creating the function works fine but executing CREATE AGGREGATE produces the following error:
ERROR: function array_append(anyarray, anyelement, integer) does not exist
I am not able to resolve this error.
Maybe this will help:
CREATE OR REPLACE FUNCTION _final_nth( anyarray, integer )
RETURNS anyelement AS
$BODY$
SELECT a
FROM unnest( $1 ) a
ORDER BY a
offset $2
LIMIT 1;
$BODY$
Use it without an extra aggregate-function.
SELECT _final_nth(array_agg(someelement),n) AS someelement
FROM sometable
GROUP BY someelement;
n is the offset.

Related Links

SAP Hana - STRING_AGG issue with ORDER BY CLAUSE
rrdtool does not keep max
Tableau Aggregate Percentage
use aggregates (min, max, avg) in CONSTRUCT query [duplicate]
R: transposing and aggregating columns
SQL Query Aggregate Error
Usage of aggregate functions in MarkLogic
Querying customDimensions in Google Bigquery
NHibernate - QueryOver criteria appearing in Where instead in Having clause, error
Howto aggregate on full data set in Spring Batch jobs?
Creating an aggregate function fails
Necessity of declaration of function in c and cpp
Calculate window average in tableau
Complexity asymptotic relation (theta, Big O, little o, Big Omega, little omega) between functions
What are TOP_COUNT and TOP_MAXCOUNT in BigQuery?
marklogic need advise for approach to aggregate documents

Categories

HOME
asp.net
url
stanford-nlp
qemu
jenkins-pipeline
webvr
reference
where
uml-designer
country-codes
sahi
phpexcel
digital
captcha
mongodb-replica-set
drush
fine-uploader
openstack-horizon
cucumberjs
google-calendar-api3
v4l2
poisson
waterfall
aws-certificate-manager
cloudformation
azure-table-storage
foxpro
jmsserializerbundle
loopback
unity3d-5
traitsui
minitest
outlook-2010
extern
device-driver
framebuffer
jquery-inputmask
google-chrome-console
vertical-alignment
redbean
google-cloud-powershell
java-5
compiler-warnings
colorbox
npoco
qtwebkit
stream-socket-client
stanford-nlp-server
denodo
openxava
multi-targeting
multi-select
trusted-computing
wicketstuff
catalyst
group
sharing
indoor-positioning-system
universe
class-design
boost-range
nd4j
cexception
jsondoc
wordpress-plugin
jersey-test-framework
sdkman
database-project
jain-sip
roslyn-code-analysis
playn
page-refresh
tinymce-plugins
tidesdk
genome
chrome-mobile
lwuit
powerpoint-2010
worklight-appcenter
dotcmis
debuggervisualizer
satisfiability
sigma-grid-control
fastcgi-mono-server
ie-developer-tools
static-class
magicsuggest
brew
qt5.1
invalidoperationexception
fileoutputstream
mcpd
httppostedfilebase
flexbuilder
xcdatamodel
text-manipulation
simultaneous
jquery-attributes
miniport
newspeak
data-formats
atmega16
httpbrowsercapabilities

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App