~/devreads

#window functions

9 posts

14 Feb 2019

lukaseder 1 min read

A fun report to write is to calculate a cumulative percentage. For example, when querying the Sakila database, we might want to calculate the percentage of our total revenue at any given date. The result might look like this: Notice the beautifully generated data. Or as raw data: payment_date |amount |percentage -------------|--------|---------- 2005-05-24 |29.92 |0.04 … Continue reading How to…

sqlcumulative percentagecumulative sumwindow functions

3 Sept 2018

lukaseder 1 min read

I’ve stumbled across this fun SQL question on reddit, recently. The question was looking at a time series of data points where some events happened. For each event, we have the start time and the end time timestamp start end ----------------------------------- 2018-09-03 07:00:00 1 null 2018-09-03 08:00:00 null null 2018-09-03 09:00:00 null null 2018-09-03 10:00:00 … Continue reading Find the…

sqlgapstime serieswindow functions

27 Jun 2018

lukaseder 1 min read

Imagine you have a configuration table like the following: It specifies a set of rules that Can be enabled / disabled Can be given a priority among themselves Include a set of flags which correspond to the thing you want to configure (e.g. some check to execute) Those flags can be ordered as well So, … Continue reading Using UNPIVOT…

sqlconfiguration tablenormalisationunpivotwindow functions

31 Oct 2016

lukaseder 1 min read

I’m frequently telling developers to put window functions almost everywhere, because they’re so awesome! One feature that I rarely see in the wild (even if it is extremely useful for reporting) is called “logical windowing” in Oracle, and it’s most useful when used with INTERVAL ranges. Let’s see what we may want to do. I … Continue reading A Little…

sqlframes clauselogical windowsliding windowwindow functions

24 Mar 2016

lukaseder 1 min read

This Stack Overflow question has yet again nerd-sniped me [finding the] maximum element in the array that would result from performing all M operations Here’s the question by John that was looking for a Java solution: With an array of N elements which are initialized to 0. we are given a sequence of M operations … Continue reading Time for…

sqlcommon table expressionsprefix sumwindow functions

26 Jan 2016

lukaseder 1 min read

Recently, we’ve published our article about the awesome window function support in jOOλ 0.9.9, which I believe is some of the best additions to the library that we’ve ever done. Today, we’ll look into an awesome application of window functions in a use-case that is inspired by this Stack Overflow question Sean Nguyen: How to … Continue reading How to…

java 8joojavawindow functions

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

17 Dec 2015

lukaseder 1 min read

The following is a very common problem in all data related technologies and we’re going to look into two very lean, SQL-based solutions for it: How do I fill the cells of a sparse data set with the “previous non-empty value”? The problem The problem is really simple and I’m reusing the example provided by … Continue reading How to…

sqlmodel clauseoraclesparse datawindow functions

7 Nov 2015

lukaseder 1 min read

A very interesting problem that can be solved very easily with SQL is to find consecutive series of events in a time series. But what is a consecutive series of events in a time series? Take Stack Overflow, for example. Stack Overflow has a cool reputation system that uses badges to reward certain behaviour. As … Continue reading How to…

sqlconsecutive eventsstack overflowtime serieswindow functions