Re:Medium-hard SQL interview questions
by meritt on Saturday May 02, 2020 @11:44PM
(#23056715)
> It's better to use windowing functions, I believe it should be faster than self-join.
Your statement is making the assumption you have completely dense data and you can simply offset a number of rows to get the desired denominator. Sparse data is a very common occurrence, and now your MoM/YoY/XoX queries are completely incorrect.
Re:Medium-hard SQL interview questions
by radiowave on Sunday May 03, 2020 @06:11AM
(#23058071)
Good point. I often find the easiest approach is to join against a sequence to 'unsparsify' the data, then use the window function. I'd guess this is likely still faster than a self-join, unless the data is very sparse.
Your statement is making the assumption you have completely dense data and you can simply offset a number of rows to get the desired denominator. Sparse data is a very common occurrence, and now your MoM/YoY/XoX queries are completely incorrect.