我有一张玩具桌:
hive> SELECT * FROM ds.forgerock;
OK
forgerock.id forgerock.productname forgerock.description
1 OpenIDM Platform for building enterprise provisioning solutions
2 OpenAM Full-featured access management
3 OpenDJ Robust LDAP server for Java
4 OpenDJ desc2
4 OpenDJ desc2
Time taken: 0.083 seconds, Fetched: 5 row(s)
我想找个像这样的表:
id flag
1 0
2 0
3 1
4 1
我正在使用玩具表来迭代和开发工作代码。
SELECT id, CASE WHEN "OpenDJ" IN COLLECT_SET(productname) THEN 1 ELSE 0 END AS flag,
GROUP BY id FROM ds.forgerock;
请注意,在玩具数据集中,每个id
只有一个不同的值,因此COLLECT_SET
似乎没有必要。但是,鉴于实际数据集实际上有多个不同的值,我想要做的事情会更有意义。
答案 0 :(得分:1)
使用max()
通过id:
SELECT id, max(CASE WHEN productname='OpenDJ' THEN 1 ELSE 0 END) AS flag
FROM ds.forgerock
GROUP BY id;