~/devreads

#aggregate functions

9 posts

3 Jun 2024

lukaseder 1 min read

A cool standard SQL:2003 feature is the aggregate FILTER clause, which is supported natively by at least these RDBMS: The following aggregate function computes the number of rows per group which satifsy the FILTER clause: This is useful for pivot style queries, where multiple aggregate values are computed in one go. For most basic types … Continue reading Emulating SQL…

sqlaggregate functionsfilterfilter clausejson

1 Mar 2024

lukaseder 1 min read

I’ve blogged about generic ways of getting top 1 or top n per category queries before on this blog. An Oracle specific version in that post used the arcane KEEP syntax: This is a bit difficult to read when you see it for the first time. Think of it as a complicated way to say … Continue reading Getting Top…

sqlaggregate functionsany valuekeeporacle

6 Feb 2023

lukaseder 1 min read

I’ve found an interesting question on Twitter, recently. Is there any performance impact of using FILTER in SQL (PostgreSQL, specifically), or is it just syntax sugar for a CASE expression in an aggregate function? As a quick reminder, FILTER is an awesome standard SQL extension to filter out values before aggregating them in SQL. This … Continue reading The Performance…

sqlaggregate functionsbenchmarkfilterperformance

8 Feb 2021

lukaseder 1 min read

So, @rotnroll666 nerd sniped me again. Apparently, the Neo4j Cypher query language supports arbitrary reductions, just like any functional collection API, oh say, the JDK Stream API: SQL doesn’t have this, yet it would be very useful to be able to occasionally do that. An arbitrary reduction can be implemented “easily” in SQL. Let’s look … Continue reading Implementing a…

jooq-developmentsqlaggregate functionscustom aggregate functionfunctional programming

28 Jan 2019

lukaseder 1 min read

In my previous article, I showed what the very useful percentile functions (also known as inverse distribution functions) can be used for. Unfortunately, these functions are not ubiquitously available in SQL dialects. As of jOOQ 3.11, they are known to work in these dialects: Dialect As aggregate function As window function MariaDB 10.3.3 No Yes … Continue reading How to…

sqlaggregate functionsanalyticsinverse distribution functionordered-set aggregate function

9 Oct 2018

lukaseder 1 min read

All SQL databases support the standard aggregate functions COUNT(), SUM(), AVG(), MIN(), MAX(). Some databases support other aggregate functions, like: EVERY() STDDEV_POP() STDDEV_SAMP() VAR_POP() VAR_SAMP() ARRAY_AGG() STRING_AGG() But what if you want to roll your own? Java 8 Stream Collector When using Java 8 streams, we can easily roll our own aggregate function (i.e. a … Continue reading Writing Custom…

java 8sqlaggregate functionaggregate functionscollector

6 Jan 2016

lukaseder 1 min read

You heard right. Up until now, the awesome window functions were a feature uniquely reserved to SQL. Even sophisticated functional programming languages still seem to lack this beautiful functionality (correct me if I’m wrong, Haskell folks). We’ve written tons of blog posts about window functions, evangelising them to our audience, in articles like: Probably the … Continue reading 2016 Will…

java 8jooaggregate functionscollectorswindow functions

6 Jan 2015

lukaseder 1 min read

Some databases are awesome enough to implement the MEDIAN() aggregate function. Remember that the MEDIAN() is sligthly different from (and often more useful than) the MEAN() or AVG() (average). While the average is calculated as the SUM(exp) / COUNT(exp), the MEDIAN() tells you that 50% of all values in the sample are higher than the … Continue reading How to…

sqlaggregate functionsaverageinverse distribution functionsjooq

30 Dec 2014

lukaseder 1 min read

Sometimes when aggregating data with SQL, we’d love to add some additional filters. For instance, consider the following world bank data: GDP per capita (current US$) 2009 2010 2011 2012 CA 40,764 47,465 51,791 52,409 DE 40,270 40,408 44,355 42,598 FR 40,488 39,448 42,578 39,759 GB 35,455 36,573 38,927 38,649 IT 35,724 34,673 36,988 33,814 … Continue reading The Awesome…

sqlaggregate functionsfilter clausejooqpostgresql