hive 如果两个列的值为“是”,则SQL中的新列应为“是”

wko9yo5t  于 2023-04-06  发布在  Hive
关注(0)|答案(2)|浏览(136)

我有下表:
| 姓名|A|B|C|
| --------------|--------------|--------------|--------------|
| X|是|N|是|
| Z|N|是|N|
我想创建一个新的列Res,如果A B C中的任何两列是Y,则Res应该是Y,否则应该是N
| 姓名|A|B|C|雷斯|
| --------------|--------------|--------------|--------------|--------------|
| X|是|N|是|是|
| Z|N|是|N|N|
PS:我有三个以上的栏目要检查

dz6r00yl

dz6r00yl1#

您可以尝试:

SELECT Name, A, B, C, 
    CASE 
        WHEN 'Y' IN (A,B) OR 'Y' IN (A,C) OR 'Y' IN (B,C) THEN 'Y'
        ELSE 'N'
    END AS Res
FROM [table_name];

如果你有很多列,并且可以使用UNPIVOT,你可以这样做:

SELECT u.Name
FROM 
(
    SELECT Name, A, B, C
    FROM your_table_name
) DS
UNPIVOT 
(
    Res FOR Col IN (A, B, C)
) UNPVT
WHERE UNPVT.Res = 'Y'
GROUP BY UNPVT.Name
HAVING COUNT(DISTINCT u.Col) >= 2;
x4shl7ld

x4shl7ld2#

SELECT *, case when A = "Y" AND B = "Y" then "Y"
               when B = "Y" AND C = "Y" then "Y"
               when A = "Y" AND C = "Y" then "Y"
               else "N"
               end as Res
FROM your_table

相关问题