三村です。 caseに関しては http://www.postgresql.jp/document/pg743doc/html/functions-conditional.html#AEN11378 を参照してみてください