如果类型正确,则设置属性值 [英] Set attribute value if correct type

查看:68
本文介绍了如果类型正确,则设置属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我需要根据类型设置属性的值。为了使可视化一点,我有此表(让我们将表称为 预测 c):

So I have a case where I need to set the value of an attribute depending on the type. To visualize a little bit, I have this table (lets call the table 'forecasts'):

| creation_time | id |    d_type    |    d_amount  |
|---------------|--- |--------------|--------------|
|    1534842000 |  1 |           2  |          1.3 |
|    1534842000 |  2 |           3  |          0.3 |

d_type 表示它是哪种类型,因此,例如,假设:

The d_type means which type it is, so for example, let's say:

1 = nothing
2 = rain
3 = snow

我如何获得看起来像这样的表:

How could I get a table that looks something like:

| creation_time | id |    rain      |    snow      |
|---------------|--- |--------------|--------------|
|    1534842000 |  1 |          1.3 |            0 |
|    1534842000 |  2 |            0 |          0.3 |

也就是说,属性的值应为 d_amount 如果属性对应于正确的 d_type

That is, the attribute should have a the value of d_amount if the attribute corresponds to the right d_type.

我尝试过这样的事情:

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;

但是它返回一个我不想要的大小写列。我是PostgreSQL的新手,非常抱歉。感谢您的帮助!

But it returns a case-column that I don't want. I'm new to PostgreSQL so sorry for noobing this down. Thankful for any help!

推荐答案

尝试

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;

这篇关于如果类型正确,则设置属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆