从右到左将数字代码转换为字符串

Converting a numerical code into a word string right-to-left

我正在尝试使用 SQL select 表达式将以下内容从第一列转换为第二列。

Code        Outcome
88881133    Species 1, 2, 3, 4 sick
88888888    NULL
88888833    Species 1, 2 sick
88888811    Species 1, 2 sick
88888111    Species 1, 2, 3 sick
88888881    Species 1 sick

代码应从右向左阅读。
1 或 3 表示该物种生病了。
8表示物种没有生病。

我想它涉及一些 CASE 表达式,但我不能更进一步:

SELECT 
CASE WHEN RIGHT(Code, 1) = 1 OR WHEN RIGHT(Code, 1) = 3 
THEN 'Species 1 sick' END AS Outcome
FROM table

我用的是Vertica数据库

请尝试该查询 - 它应该可以解决问题 - 请将 t_tab 更改为 your_table_name 它应该可以工作

WITH 
t_tab2 AS 
(
SELECT t.code,
       CASE WHEN SUBSTR(t.code,1,1) IN (1,2,3) THEN 8 END Out1,
       CASE WHEN SUBSTR(t.code,2,1) IN (1,2,3) THEN 7 END Out2,
       CASE WHEN SUBSTR(t.code,3,1) IN (1,2,3) THEN 6 END Out3,
       CASE WHEN SUBSTR(t.code,4,1) IN (1,2,3) THEN 5 END Out4,
       CASE WHEN SUBSTR(t.code,5,1) IN (1,2,3) THEN 4 END Out5,
       CASE WHEN SUBSTR(t.code,6,1) IN (1,2,3) THEN 3 END Out6,
       CASE WHEN SUBSTR(t.code,7,1) IN (1,2,3) THEN 2 END Out7,
       CASE WHEN SUBSTR(t.code,8,1) IN (1,2,3) THEN 1 END Out8
FROM t_tab t
)
SELECT tt.code,
       CASE WHEN tt.out1||tt.out2||tt.out3||tt.out4||
                 tt.out5||tt.out6||tt.out7||tt.out8 IS NULL THEN NULL
            ELSE REGEXP_REPLACE(
                 REGEXP_REPLACE(RTRIM('Species' || ' ' || tt.out8 || ', ' || tt.out7|| ', ' 
                                                       || tt.out6 || ', ' || tt.out5 || ', ' 
                                                       || tt.out4 || ', ' || tt.out3 || ', ' 
                                                       || tt.out2 || ', ' || tt.out1, ', ') 
                                                       || ' sick', ', | ,', ','), ',{1,}', ', ') END AS Outcome
FROM t_tab2 tt

它给我的结果是:

1   88881133    Species 1, 2, 3, 4 sick
2   88888888    
3   88888833    Species 1, 2 sick
4   88888811    Species 1, 2 sick
5   88888111    Species 1, 2, 3 sick
6   88888881    Species 1 sick

这是另一种使用 likecase 的方法。我只是将 1 和 3 翻译成 1,将 8 翻译成 0。主要是因为我打算使用二进制方法,但这看起来更简单。真正的原因只是为了保持 case 语句简单(否则你必须同时检查 1 和 3 个案例)。

rtrim 有第二个参数,意思是 trim 只有那个额外的逗号 space。删除最后一个是一个简单的技巧。外壳只是确保有物种(否则它将 return null)。

希望对您有所帮助。

with translated_mytable as (
  select code, translate(code,'813','011') newcode 
  from mytable
)
select Code,
       case when newcode like '%1%' then
         'Species ' || 
         rtrim(case when newcode like '_______1' then '1, ' else '' end ||
               case when newcode like '______1_' then '2, ' else '' end ||
               case when newcode like '_____1__' then '3, ' else '' end ||
               case when newcode like '___ 1___' then '4, ' else '' end ||
               case when newcode like '___1____' then '5, ' else '' end ||
               case when newcode like '__1_____' then '6, ' else '' end ||
               case when newcode like '_1______' then '7, ' else '' end ||
               case when newcode like '1_______' then '8, ' else '' end, 
         ', ') || ' sick' 
       end Outcome         
from translated_mytable