HQL,如果满足条件,请插入两个行

发布于 2025-01-24 06:50:51 字数 631 浏览 2 评论 0原文

我有下表在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2025-01-31 06:50:51

您可以尝试使用联合合并或将行与bot复制。以下例如工会第一个查询选择所有记录,第二个查询仅选择bot的查询。

edit

在回答编辑的问题时,我添加了一个额外的奇偶校验列(存储10),名为原始要区分命名的重复条目

    SELECT
        p1.*,
        1 as original
    FROM
        table_persons p1
    UNION ALL
    SELECT
        p1.*,
        0 as original
    FROM
        table_persons p1
    WHERE p1.type='bot'

,然后可以将其插入您的其他表d1_info使用上述查询作为子查询或带有所需转换案例表达式的子查询或CTE,例如,

INSERT INTO d1_info
  (`db_type`, `info`, `date`)
WITH merged_data AS (
    SELECT
        p1.*,
        1 as original
    FROM
        table_persons p1
    UNION ALL
    SELECT
        p1.*,
        0 as original
    FROM
        table_persons p1
    WHERE p1.type='bot'
)
SELECT
    CONCAT('x_',CASE
           WHEN m1.type='per' THEN m1.type
           WHEN m1.original=1 AND m1.type='bot' THEN m1.type
           ELSE 'bnt'
    END) as db_type,
    CASE
       WHEN m1.type='per' THEN 'b'
       ELSE 'x'
    END as info,
    m1.date
FROM
    merged_data m1
ORDER BY m1.people,m1.date;

请参见工作演示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 with bot.

Edit

In response to the edited question, I have added an additional parity column (storing 1 or 0) named original to differentiate the duplicate entry named

    SELECT
        p1.*,
        1 as original
    FROM
        table_persons p1
    UNION ALL
    SELECT
        p1.*,
        0 as original
    FROM
        table_persons p1
    WHERE p1.type='bot'

You 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 eg

INSERT INTO d1_info
  (`db_type`, `info`, `date`)
WITH merged_data AS (
    SELECT
        p1.*,
        1 as original
    FROM
        table_persons p1
    UNION ALL
    SELECT
        p1.*,
        0 as original
    FROM
        table_persons p1
    WHERE p1.type='bot'
)
SELECT
    CONCAT('x_',CASE
           WHEN m1.type='per' THEN m1.type
           WHEN m1.original=1 AND m1.type='bot' THEN m1.type
           ELSE 'bnt'
    END) as db_type,
    CASE
       WHEN m1.type='per' THEN 'b'
       ELSE 'x'
    END as info,
    m1.date
FROM
    merged_data m1
ORDER BY m1.people,m1.date;

See working demo db fiddle here

灵芸 2025-01-31 06:50:51

我认为您想要的是创建一个捕获您逻辑的新表。这将简化您的查询并进行,因此您可以轻松添加新类型,而无需编辑案例语句的逻辑。以后查看您的逻辑也可能使其更干净。

CREATE TABLE table_persons (
  `people` VARCHAR(5),
  `type` VARCHAR(3),
  `date` VARCHAR(10)
);

INSERT INTO table_persons
VALUES
  ('lisa', 'bot', '19-04-2022'),
  ('wayne', 'per', '19-04-2022');
  


CREATE TABLE info (
  `type` VARCHAR(5),
  `db_type` VARCHAR(5),
  `info` VARCHAR(1)
);

insert into info 
   values 
   ('bot', 'x_bot', 'x'), 
   ('bot', 'x_bnt', 'x'), 
   ('per','x_per','b');

然后,您可以轻松地加入:

select 
  info.db_type, 
  info.info, 
  persons.date date 
from 
  table_persons persons inner join info 
on 
  info.type = persons.type

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.

CREATE TABLE table_persons (
  `people` VARCHAR(5),
  `type` VARCHAR(3),
  `date` VARCHAR(10)
);

INSERT INTO table_persons
VALUES
  ('lisa', 'bot', '19-04-2022'),
  ('wayne', 'per', '19-04-2022');
  


CREATE TABLE info (
  `type` VARCHAR(5),
  `db_type` VARCHAR(5),
  `info` VARCHAR(1)
);

insert into info 
   values 
   ('bot', 'x_bot', 'x'), 
   ('bot', 'x_bnt', 'x'), 
   ('per','x_per','b');

and then you can easily do a join:

select 
  info.db_type, 
  info.info, 
  persons.date date 
from 
  table_persons persons inner join info 
on 
  info.type = persons.type
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文