Postgresql Nested If Python Example