Bash Nested Case Statement Sql