SQL Window Functions
helenanderson.co.nz
helenanderson.co.nz
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.
'2019-01-01' < '2019-01-01 00:00:00.0000', so that time won't match the date. '2018-12-31' < '2018-12-31 00:00:00.000', to it will not match that either.
The rest of your comment is fairly academic because, surely, the dates are stored as a native date/time type. (Either `date` itself or `timestamp without timezone` - probably the latter given that the literals in the article mention times.) Using strings would both be inefficient cause numerous correctness problems, including, as you just said, the fact that comparison with '2018-12-31' would give different results to comparison with '2018-12-31 00:00:00.000'. There's no reason to assume that the article had chosen this insane path.
Just to humour the possibility that someone had, foolishly, used a string for this possibility: Your most recent comment is correct that as strings '2018-12-31' < '2018-12-31 00:00:00.000', so the BETWEEN in my previous comment would not have worked (although it depends, of course, on what exact strings were stored in the column!). But the comparison you suggested in your previous comment would also be incorrect, for the same reason I already objected to: `BETWEEN '2018-01-01' AND '2019-01-01'` will match (the string) '2019-01-01'.
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.
select percentile_disc(0.5) within group (order by things.value)
from thingsAlso 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.
It's almost unimaginable for me!
Am I missing something obvious?