Single line query result

Asked

Viewed 37 times

0

My database is like this:

id | municipio_id |  ano | populacao
 1 |            0 | 2010 |      5000
 2 |            0 | 2011 |      5000

I have the following query:

SELECT
case when ano = 2010 then populacao end a2010,
case when ano = 2011 then populacao end a2011
FROM populacao
where municipio_id = 0

The result comes out like this:

a2010 | a2011    
 5000 |  null
 null |  5000

I need the results to come out like this:

a2010 | a2011    
 5000 |  5000

How to do?

1 answer

2


SELECT SUM(CASE
         WHEN ano = 2010 THEN populacao
         ELSE 0
       END) AS a2010,
       SUM(CASE
         WHEN ano = 2011 THEN populacao
         ELSE 0
       END) AS a2011
 FROM populacao
WHERE municipio_id = 0
  • didn’t work out buddy. I changed my question to check my database

  • @Italorodrigo I made a change.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.