I spent all day today trying to figure out how to fix something that wasn't broken.
Layers of ASP.NET leading to a #SQL query a half dozen pages long.
<rant>
You know, SQL IS A FREAKING PROGRAMMING LANGUAGE!
You can PROGRAM with it! The procedure doesn't have to be one ginourmous query - you can break it up into modular - testable - chunks.
Temp tables are a thing in SQL - or even table variables if you don't want to have to clean up after yourself. They work great!
</rant>

I just want to rebuild the site from the bottom up. No bandwidth for that yet. Maybe in September...

@Jirikiha I had that problem with a BI system firing 36k character long queries.
Luckily my favorite database #vertica has a feature called directed queries where you can overwrite those queries. You create a pattern for the incoming BS and write a proper query the db is actually using. Due to those parameters it is kind of dynamic.
But I have to warn you: You can get crazy trying to understand what the query you get actually wants.