added events sql function. It looks goofy but works.