• There's no cost in making your code look readable, just as there's no cost in writing in paragraphs instead of one long sentence. Laying out your code will become second nature as you become more experienced.  You may even develop coding standards that everybody in your organisation has to adhere to.  In the meantime, you can use one of the tools already mentioned to prettify your code.  Another product that has a free offering is SQL Complete.

    So the database runs on a Linux server, but what type of database is it - SQL Server, Oracle or something else?  I only have experience with SQL Server, so if it's that, write a stored procedure so that all your logic is in one place and not duplicated in all Excel workbooks.  I'd be surprised if there's no equivalent in Oracle, as well.

    Coming back to your original question, though, you need to improve the performance of that query.  If it's SQL Server, please post the actual execution plan.  If it's Oracle or something else, this isn't the best place to be asking for help.

    Good luck
    John