我在PostgreSQL中有一个如下表。
+-------+--------+----------+--------+
|id | name | rate | amount |
+-------+--------+----------+--------+
|123 | Flat | | 40 |
|123 | weekly | 200 | |
+-------+--------+----------+--------+
我需要显示金额时的金额,并按照其每周的名称来评价'柱。期待这样的输出:
+-------+-------+-----------+--------+
| id | name | rate | amount |
+-------+-------+-----------+--------+
| 123 | 40 | | 40 |
| 123 | 200 | 200 | |
+-------+-------+-----------+--------+
在PostgreSQL中是否可行。如果是,那该怎么做。
答案 0 :(得分:1)
使用CASE
表达式:
SELECT id, CASE WHEN name = 'Flat' THEN amount ELSE rate END AS name, rate, amount
FROM mytable