sql - 替换列值

标签 sql sql-server

我有这张 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/

相关文章:

php - 在浏览器上使用php查看存储在数据库中的文件

PHP:从具有重复条目的数组构建 HTML 表

.net - 使用 PowerShell 从 SQL Server 中获取整行

sql - 关于此 SQL 语句的 ROLLBACK TRANSACTION 的任何想法

sql-server - 无法在 SQL Server 2017 中创建 tSQLtCLR 程序集

SQL-Server:备份集保存现有数据库以外的数据库的备份

mysql - 继承相同的 N :M table for different associations

php - 如何基于树遍历算法从这个结果集中生成 TreeView ?

sql-server - sys.dm_tran_current_transaction。 transaction_id 的唯一性如何

sql - 有没有更简单的方法将 SQL Server 中的 DateTime 设置为 23 :59:59