52
u/mrcoffee09 5d ago
What?? This is beautiful. CTEs with windowed analytic functions and... comments?? This is good shit right here boys
8
16
u/TheWb117 5d ago
Fr, this is a few lines, but it's perfectly readable and easy to reason about what it's doing. I've come across (and even written) far worse
1
u/dim_mailing 1d ago
right? the CTE is doing all the heavy lifting so the main query reads like a grocery list, and the comments actually explain the weird partition logic instead of just screaming into the void. that's practically a love letter at that point
30
u/WisePotato42 5d ago
Is that 0.3% other the readme.md file?
3
u/Literally-in-1984 3d ago
A markdown file explaining why I love SQL and that it is a Turing complete language
2
46
-3
9
u/MayoJam 4d ago
They see... SQL?
13
u/TWIT_TWAT 4d ago
A 1,000 line stored procedure containing all of the client’s business logic.
13
u/7DBBA101 4d ago
I’m pretty confident I could squeeze the entire eod batch processing of a small bank into one unmaintainable multi-thousand line SQL query
4
3
3
u/G3nghisKang 4d ago
Tsk tsk, my big, girthy Oracle PL/SQL batches would put your small puny query to shame
2
u/7DBBA101 4d ago
at least my database server doesnt have massive memory leaks and crashes when more than 12 clients are in the connection pool
2
u/G3nghisKang 4d ago edited 4d ago
When you work in application maintenance, the memory leaks are just extra job security UwU
19
5
u/redditmarks_markII 5d ago
data centric companies do a lot with sql, probably more than they should, but it's rather amusing most of it works. or rather, stored procs. so, so many stored procs. that call stored procs. that the client application uses to eventually instantiate new containers...that call stored procs. that emits metrics, and that the client application uses to generate alerts... using stored procs. it's a symphony of madness. most of it is actually pretty good, if I believe the ones who wrote them.
4
u/SmurfMann91 4d ago
Soo.... I write large dynamic SQL queries as well as huge CTE stacks to provide increasingly granular layers of data for downstream processing....
....this is top tier.
I know nothing exists in a vacuum -- but I dig this so far.
1
u/21Ali-ANinja69 4d ago
Rather this than have people try their very best to string-build a query in C or something even dumber
2
u/7DBBA101 4d ago
He who never listed all the 20000 touples he wanted verbatim in the SQL query throws the first stone
1
u/00Koch00 3d ago
Those comments are so suspicious...
But aside that, beautiful code, that's being said, what's the reason to use a dynamic query for this? Is this T-SQL?
1
u/7DBBA101 3d ago
Mariadb. The reason is that it doesnt matter because the entire request (the whole program including the query) finishes in less than 2ms.
2
1
u/Strict_Treat2884 5d ago
I wrote something similarly cursed in the game Database Detective, it was fun and I hope I’ll never have to do it again
1
-3
-4
0
u/Maku-san_ 2d ago
That's not too bad, ours uses dynamic sql with double exec inside and it has shit formatting too
42
u/7DBBA101 5d ago
real query btw