英文:
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'm drawn to the conclusion that it isn't (currently?) supported.
Also there does not seem to be "if" of "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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论