Implemented event sorting in SQL