CnosDB支持CASE-WHEN查询吗?

huangapple go评论41阅读模式
英文:

Does CnosDB support CASE-WHEN queries?

问题

以下是翻译好的内容:

我有一个数据集,其原始值是整数。

SQL 查询很简单:

SELECT last("DischargingBattery") FROM battery

我想将非零数值转换为 "BUSY" 字符串,将零数值转换为 "IDLE"。SQL 查询应该类似于以下内容:

SELECT last("DischargingBattery"),

    CASE
        WHEN last("DischargingBattery") = 0 THEN 'IDLE'
        ELSE 'BUSY'
    END AS DischargingBatteryStatus

FROM battery

然而,我尚未在文档中找到相关参考资料。

是否有任何解决方法?

英文:

I have a data set whose original values are integers.

The SQL is straightforward:

SELECT last("DischargingBattery") FROM battery

I would like to convert the none zero numbers into "BUSY" string and zero number into "IDLE". The SQL would like similar to following:


SELECT last("DischargingBattery") ,

    CASE
        WHEN last("DischargingBattery") = 0 THEN 'IDLE'
        ELSE 'BUSY'
    END AS DischargingBatteryStatus

FROM battery

However, I didn't find any reference in the doc yet.

Any workaround?

答案1

得分: 1

I can not find any reference to case in any CnosDB docs either, so I'm drawn to the conclusion that it isn't (currently?) supported.

Also there does not seem to be "if" or "iif" with the select clause either.

So, in your case all I can suggest as an alternative is (but I dislike it):

SELECT
      0      as DischargingBattery
    , 'IDLE' AS DischargingBatteryStatus
FROM battery
WHERE last("DischargingBattery") = 0

UNION ALL

SELECT
      last("DischargingBattery")
    , 'BUSY' DischargingBatteryStatus
FROM battery
WHERE last("DischargingBattery") <> 0

NB: I am not familiar with the function `last`


<details>
<summary>英文:</summary>

I can not find any reference to `case` in any CnosDB docs either, so I&#39;m drawn to the conclusion that it isn&#39;t (currently?) supported.

Also there does not seem to be &quot;if&quot; of &quot;iif&quot; with the select clause either.

So, in your case all I can suggest as an alternative is (but I dislike it):


    SELECT
          0      as DischargingBattery
        , &#39;IDLE&#39; AS DischargingBatteryStatus
    FROM battery
    WHERE last(&quot;DischargingBattery&quot;) = 0
    
    UNION ALL
    
    SELECT
          last(&quot;DischargingBattery&quot;)
        , &#39;BUSY&#39; DischargingBatteryStatus
    FROM battery
    WHERE last(&quot;DischargingBattery&quot;) &lt;&gt; 0

NB: I am not familiar with the function `last`

</details>



huangapple
  • 本文由 发表于 2023年3月8日 14:47:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定