How do you write optimized SQL queries using complex joins and window functions for Allen Integrated Solutions?