Here is an example table called animal
:
name | color
------------
fox | brown
fox | red
dog | gold
Now, what I want is this result:
fox | dog
-------------
brown | gold
red |
The names should be columns of the result with the different color values as rows.
My first thought was like:
SELECT color
FROM animal
WHERE name='fox'
[some sort of join?]
SELECT color
FROM animal
WHERE name='dog'
But I don't know what kind of join would do the trick.
Second thought:
SELECT CASE WHEN name = 'fox' THEN color ELSE NULL END AS fox,
CASE WHEN name = 'dog' THEN color ELSE NULL END AS dog
FROM animal
This returns:
fox | dog
-----------
red |
brown |
| gold
I would like to move the null values in this table to the end. I tried to:
ORDER BY CASE name
WHEN 'fox' THEN fox
WHEN 'dog' THEN dog
END
But I'm not sure if this is really what I want and Postgres is nagging that fox is not a column although I can do ORDER BY fox
.
Maybe my approach is total nonsense or there is some kind of coalesce magic that can do the trick?