gpt4 book ai didi

sql - Oracle SQL : Alternative to nested REPLACE() in Select statement?

转载 作者:行者123 更新时间:2023-12-04 21:49:19 25 4
gpt4 key购买 nike

所以我有一个看起来像这样的查询:

SELECT IncidentNumber, 
ToiletType,
ToiletDangers,
IncidentDate
FROM Core.LostLawsuits

...返回以下结果,类似于:
+----------------+------------+---------------------------+---------------+
| IncidentNumber | ToiletType | ToiletDangers | Incident Date |
+----------------+------------+---------------------------+---------------+
| 2100 | A | Spontaneous Combustion | 2/1/2016 |
+----------------+------------+---------------------------+---------------+
| 2101 | P | Attracts Bear Stampede | 2/1/2016 |
+----------------+------------+---------------------------+---------------+

我想做的是获取结果,但在输出时更改 ToiletType 列结果。目前,我正在使用嵌套的 REPLACE() ,并想知道是否有更好/不同的方法来做到这一点, 同时为 ToiletType 保持一列结果 :
SELECT IncidentNumber, 
REPLACE(REPLACE(ToiletType, 'A', 'Automatic Standard'), 'P', 'Portable Potty') as ToiletType,
ToiletDangers,
IncidentDate
FROM Core.LostLawsuits

最佳答案

CASE 表达式适用于您的示例:

case ToiletType
when 'A' then 'Automatic Standard'
when 'P' then 'Portable Potty'
end

IE。
SELECT IncidentNumber, 
case ToiletType
when 'A' then 'Automatic Standard'
when 'P' then 'Portable Potty'
end as ToiletType,
ToiletDangers,
IncidentDate
FROM Core.LostLawsuits

也许更好的是加入 ToiletTypes 表?

关于sql - Oracle SQL : Alternative to nested REPLACE() in Select statement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35134114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com