我有这张 table ,
Name Rating
A 2
B 1
C 5
D 3
E 1
F 4
我有一个评级系统
1-Excellent, 2-Very Good, 3-Good, 4-OK, 5-Poor
我想知道是否可以替换表中的数值以获得以下结果表。
Name Rating
A Very Good
B Excellent
C Poor
D Good
E Excellent
F OK
谢谢
最佳答案
我认为就地更新数据不是个好主意,最好存储评级的 ID,而不是数据的文本表示形式。但您可以查询表并将 int 替换为文本:
select
Name,
case Rating
when 1 then 'Excellent'
when 2 then 'Very Good,'
when 3 then 'Good'
when 4 then 'OK'
when 5 then 'Poor'
end as Rating
from <your table>
或者您可以创建一个查找表并与其连接
create table Rating (id int, desc nvarchar(128))
insert into Rating (id, desc)
select 1, 'Excellent' union all
select 2, 'Very good' union all
select 3, 'Good' union all
select 4, 'OK' union all
select 5, 'Poor'
select
t.Name,
R.desc as Rating
from <your table> as t
left outer join Rating as R on R.id = t.Rating
关于sql - 替换列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29277336/