简体   繁体   English

在 SQL 中使用 Case When 返回不同的值

[英]Using Case When in SQL to return different values

I would like to join various table and convert some column values from integer to text like below case.我想加入各种表格并将一些列值从 integer 转换为文本,如下例所示。 Let's say I have Type_ID: 1, 2, 100, 200, 300, .....假设我有Type_ID: 1, 2, 100, 200, 300, .....

If the Type_ID are 1 and 200, then it will print out High and Good respectively.如果 Type_ID 为 1 和 200,则将分别打印出 High 和 Good。 But if the Type_ID are not 1 and 200, I want it to return original integer value.但如果 Type_ID 不是 1 和 200,我希望它返回原始值 integer。 How can I do that?我怎样才能做到这一点? Thanks.谢谢。

SELECT A.AID, 
CASE WHEN B.Type_ID = '1' THEN 'High' WHEN B.Type_ID = '200' THEN 'GOOD' ELSE (Print Original number value) END AS 'Type'
FROM A, B
WHERE A.AID = B.AID

Expected Result:预期结果:

AID援助 Type类型
1 1个 High高的
2 2个 499 499
3 3个 Good好的
4 4个 Good好的
5 5个 100 100
6 6个 300 300

If I understand correctly, the else value should just be the Type_ID :如果我理解正确, else 值应该只是Type_ID

SELECT A.AID, 
       CASE B.Type_ID WHEN '1' THEN 'High'
                      WHEN '200' THEN 'GOOD'
                      ELSE CAST(B.Type_ID AS CHAR(50)) END AS Type
FROM A
INNER JOIN B ON A.AID = B.AID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM