Adding fancy sql look back in

Accidentally left it out when doing the Mako templating stuff.