151 points · by helenanders26 · 7y ago · 43 comments
IMHO it is really helpful to know these.
For some data-engineering type of work such as sessionization, doing it without window functions would make the task really complicated.
In some platforms such as MySQL there are alternatives such as correlated subqueries that also allow to do extensive data manipulation easily, but at quite the cost penalty.
In my experience, people who know window functions, are already quite well versed in SQL and thus can serve as a good proxy to gauge overall experience in Analytical SQL.
second time for calculating changes between two rows and combining that with some business rules, the existence of LAG was absolutely crucial for keeping it all in the database.
RANK / PARTITION BY is something that when you need it you really really need it. Like if you join a bunch of rows to a single column but would like to just have the first or last of those rows for results and disregard all the other rows? RANK and order by
Basically if you know that something exists, but don't know about it you can still go learn it and apply it to solve a problem.
But if you don't know about it, most likely you'll try to reinvent the wheel and spend way too much time doing it.
Reading up on (overview of) all the SQL functions is a good start.
Wikipedia has some data on support: https://en.wikipedia.org/wiki/Select_(SQL)#Window_function_s...
> BETWEEN '2018-01-01 00:00:00:000' and '2018-12-31 00:00:00:000'
So the last day of december will be missing.
It would be safer, and clearer, by doing the half-open comparison explicitly. But I would argue it's BETWEEN that's hard (because it's closed rather than half-open as you'd expect) rather than dates, at least in this case.
orderdate >= '2018-01-01 00:00:00:000' AND orderdate < '2019-01-01 00:00:00:000'
However, I agree that making `BETWEEN` use closed intervals is one of the many design-level mistakes on SQL.
`BETWEEN '2018-01-01' AND '2018-12-31'`, which as you say is just shorthand for what the article says, will correctly match '2018-12-31' (again, assuming the time in all values is precisely 00:00:00.000) and will not '2019-01-01', which is the correct behaviour.
The exclude clause is part of the "window" configuration which defines which row to process, with it you can for instance define a range between all the previous and next rows and check if your data-point looks abnormal.
I don't find a whole of use for them now but, I believe simply being aware they exist and an idea of what they do is easy enough to research and apply to any project you are working on. This is from a analyst and dev perspective.
If you have a table of metadata about your files (zipped_size and location, for example) you can window on the sum of the zipped_size to collect some number of files you can get that will be under the file size limit.
Also if you want to get really fancy, it's always worth remembering that you can write your own aggregates in Postgres, which can of course work as window functions.
Also, in Hive I used to use Custom Reducers for many things where now I have to make do with Window Functions.
BigQuery is blazingly fast though so it seems a reasonable sacrifice.
Am I missing something obvious?