# Grouping Date by Custom Hours Leads to Long SQL

**Dogan Demir**Feb 16, 2018 2:09 PM

Hello all,

I have a student activity data set that I'm trying to group into class/break-time periods. The periods are 50 min + 5 min break each. Based on the advice on this forum, I used IF/ELSEIF statements. While this works up to about halfway, in its entirety it leads to an extremely long query (about 400K characters) which breaks Amazon Athena DB (200K limit). Any suggestions on how to improve this would be greatly appreciated. Thanks!

IF ([Play Begin Hour] == 7 AND [Play Begin Minute] <= 59) THEN '7:10-8:00a'

ELSEIF ([Play Begin Hour] == 8) AND [Play Begin Minute] < 50) THEN '8:00-8:50a'

ELSEIF ([Play Begin Hour] == 8 AND [Play Begin Minute] < 55) THEN '8:50-8:55a'

ELSEIF (([Play Begin Hour] == 8 AND [Play Begin Minute] <= 59) OR ([Play Begin Hour] == 9 AND [Play Begin Minute] < 45)) THEN '8:55-9:45a'

ELSEIF ([Play Begin Hour] == 9 AND [Play Begin Minute] < 50) THEN '9:45-9:50a'

ELSEIF (([Play Begin Hour] == 9 AND [Play Begin Minute] <= 59) OR ([Play Begin Hour] == 10 AND [Play Begin Minute] < 40)) THEN '9:50-10:40a'

ELSEIF ([Play Begin Hour] == 10 AND [Play Begin Minute] < 45) THEN '10:40-10:45a'

ELSEIF (([Play Begin Hour] == 10 AND [Play Begin Minute] <= 59) OR ([Play Begin Hour] == 11 AND [Play Begin Minute] < 35)) THEN '10:45-11:35a'

ELSEIF ([Play Begin Hour] == 11 AND [Play Begin Minute] < 40) THEN '11:35-11:40a'

ELSEIF (([Play Begin Hour] == 11 AND [Play Begin Minute] <= 59) OR ([Play Begin Hour] == 12 AND [Play Begin Minute] < 30)) THEN '11:40-12:30p'

ELSEIF ([Play Begin Hour] == 12 AND [Play Begin Minute] < 35) THEN '12:30-12:35p'

ELSEIF (([Play Begin Hour] == 12 AND [Play Begin Minute] <= 59) OR ([Play Begin Hour] == 13 AND [Play Begin Minute] < 25)) THEN '12:35-1:25p'

ELSEIF ([Play Begin Hour] == 13 AND [Play Begin Minute] < 30) THEN '1:25-1:30p'

ELSEIF (([Play Begin Hour] == 13 AND [Play Begin Minute] <= 59) OR ([Play Begin Hour] == 14 AND [Play Begin Minute] < 20)) THEN '1:30-2:20p'

ELSEIF ([Play Begin Hour] == 14 AND [Play Begin Minute] < 25) THEN '2:20-2:25p'

ELSEIF (([Play Begin Hour] == 14 AND [Play Begin Minute] <= 59) OR ([Play Begin Hour] == 15 AND [Play Begin Minute] < 15)) THEN '2:25-3:15p'

ELSEIF (([Play Begin Hour] == 15 AND [Play Begin Minute] <= 59) OR ([Play Begin Hour] == 16 AND [Play Begin Minute] < 05)) THEN '3:15-4:05p'

ELSE '0' END