fix sql query for availabe events, with if stmt