Some postgres aggreation queries written for great justice