Fixed sql statements with tallying wave count and records