HQL,如果满足条件,请插入两个行
我有下表在 hive 中称为table_person:
+--------+------+------------+
| people | type | date |
+--------+------+------------+
| lisa | bot | 19-04-2022 |
| wayne | per | 19-04-2022 |
+--------+------+------------+
如果类型为“ bot”,我必须在表D1_info中添加两个行,如果类型为“ per”,我只需要添加一个行行因此,结果是:
+---------+------+------------+
| db_type | info | date |
+---------+------+------------+
| x_bot | x | 19-04-2022 |
| x_bnt | x | 19-04-2022 |
| x_per | b | 19-04-2022 |
+---------+------+------------+
如果满足此条件,该如何添加两个行? 有可能什么时候?
I have the following table called table_persons in Hive:
+--------+------+------------+
| people | type | date |
+--------+------+------------+
| lisa | bot | 19-04-2022 |
| wayne | per | 19-04-2022 |
+--------+------+------------+
If type is "bot", I have to add two rows in the table d1_info else if type is "per" i only have to add one row so the result is the following:
+---------+------+------------+
| db_type | info | date |
+---------+------+------------+
| x_bot | x | 19-04-2022 |
| x_bnt | x | 19-04-2022 |
| x_per | b | 19-04-2022 |
+---------+------+------------+
How can I add two rows if this condition is met?
with a Case When maybe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用联合合并或将行与
bot
复制。以下例如工会第一个查询选择所有记录,第二个查询仅选择bot
的查询。edit
在回答编辑的问题时,我添加了一个额外的奇偶校验列(存储
1
或0
),名为原始
要区分命名的重复条目,然后可以将其插入您的其他表
d1_info
使用上述查询作为子查询或带有所需转换案例表达式的子查询或CTE,例如,请参见工作演示DB Fiddle
You may try using a union to merge or duplicate the rows with
bot
. The following eg unions the first query which selects all records and the second query selects only those withbot
.Edit
In response to the edited question, I have added an additional parity column (storing
1
or0
) namedoriginal
to differentiate the duplicate entry namedYou may then insert this into your other table
d1_info
using the above query as a subquery or CTE with the desired transformations CASE expressions egSee working demo db fiddle here
我认为您想要的是创建一个捕获您逻辑的新表。这将简化您的查询并进行,因此您可以轻松添加新类型,而无需编辑案例语句的逻辑。以后查看您的逻辑也可能使其更干净。
然后,您可以轻松地加入:
I think what you want is to create a new table that captures your logic. This would simplify your query and make it so you could easily add new types without having to edit logic of a case statement. It may also make it cleaner to view your logic later.
and then you can easily do a join: