Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 如果类型正确,则设置属性值_Postgresql - Fatal编程技术网

Postgresql 如果类型正确,则设置属性值

Postgresql 如果类型正确,则设置属性值,postgresql,Postgresql,因此,我需要根据类型设置属性的值。为了稍微形象化一点,我有这个表(让我们称之为表“预测””): d_type表示它是哪种类型,例如: 1 = nothing 2 = rain 3 = snow 我怎样才能得到一张像这样的桌子: | creation_time | id | rain | snow | |---------------|--- |--------------|--------------| | 1534842000 | 1 |

因此,我需要根据类型设置属性的值。为了稍微形象化一点,我有这个表(让我们称之为表“
预测”
”):

d_type
表示它是哪种类型,例如:

1 = nothing
2 = rain
3 = snow
我怎样才能得到一张像这样的桌子:

| creation_time | id |    rain      |    snow      |
|---------------|--- |--------------|--------------|
|    1534842000 |  1 |          1.3 |            0 |
|    1534842000 |  2 |            0 |          0.3 |
也就是说,如果属性对应于右侧的
d\u类型
,则该属性的值应为
d\u amount

我试过这样的方法:

SELECT
        creation_time,
        id,
        "rain", CASE d_type WHEN 2 THEN d_amount END,
        "snow", CASE d_type WHEN 3 THEN d_amount END,
FROM forecasts;
但它返回一个我不想要的case列。我是新加入PostgreSQL的,所以很抱歉没有把这个记录下来。谢谢你的帮助

试试看

SELECT
    creation_time,
    id,
    CASE d_type WHEN 2 THEN d_amount END as "rain",
    CASE d_type WHEN 3 THEN d_amount END as "snow",
  FROM forecasts;

哦,就这么简单!但是,如果d_类型不匹配,则值为null。我需要它们为0,但也许我可以再添加一个case是的,只需要添加“ELSE 0”或“ELSE null”
SELECT
    creation_time,
    id,
    CASE d_type WHEN 2 THEN d_amount END as "rain",
    CASE d_type WHEN 3 THEN d_amount END as "snow",
  FROM forecasts;