【问题标题】:SQL - adding category to string value (mapping table)SQL - 将类别添加到字符串值(映射表)
【发布时间】:2020-07-30 15:58:30
【问题描述】:

我正在尝试将我之前从外部 excel 文件获得的映射复制到 SQL 查询中。 我有特定的错误作为字符串(例如“aborted”、“timeout”)。一个简化的例子:

count   last_error
452     user_aborted
889     timeout
212     request_denied
98      blacklisted_by_admin
789     login_unsuccessful
340     country_not_available

我想将这些映射到我定义的类别中,这样结果将是一个带有错误类别的新列:

count   last_error                 error_category
452     user_aborted               user 
889     timeout                    tech
212     request_denied             risk
98      blacklisted_by_admin       risk
789     login_bad                  user
340     country_not_available      tech   

这样做的最佳方法是什么?我有大约 40 个错误和六个类别。

【问题讨论】:

  • 添加新列并更新表格。

标签: sql database mapping


【解决方案1】:

你可以这样做case这样的声明

case 
    when last_error in ('user_aborted', 'login_bad') then 'user'
    when last_error in ('request_denied', 'blacklisted_by_admin') then 'risk'
    when last_error in ('timeout', 'country_not_available') then 'tech'
end as error_category

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2021-11-09
    • 2019-06-09
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多