我有一个名为summary
的表,其中列 - SQL 中的列 -Team Name
和Result
。
示例值:
Team Name | Result
team one | team one won by 10 points
Champions | Champions lost by 15 points
Patriots | Match tied
如何在单独的列中仅显示"赢"、"输"或"平"字样?团队名称可能有多个单词。
提前谢谢。
这里有一种方法:
select (case when s.result like '% won %' then 'won'
when s.result like '% lost %' then 'lost'
when s.result like '% tied%' then 'tied'
end) as result
from t;
如果团队名称包含"赢"、"输"或"平局"字样,这可能会有一些问题。 这似乎不太可能,但您可以通过以下方式防止它:
select (case when s.result like team_name || ' ' || ' won %' then 'won'
when s.result like team_name || ' ' || ' lost %' then 'lost'
when s.result = team_name || ' tied' then 'tied'
end) as result
from t;
下面的SQL代码(使用instr函数):
select s.*, case
when instr(upper(s.result),'WON')>0 then 'W'
when instr(upper(s.result),'LOST')>0 then 'L'
when instr(upper(s.result),'TIED')>0 then 'T'
else '?' end as res
from summary s