如何将这些记录分为单个新记录?

发布于 2025-01-27 00:00:21 字数 2111 浏览 5 评论 0原文

我想搜索列中是否存在一个字符串(site_id),然后将该字符串放在新表中,并在该行中具有其前列的值。该表只有2列,site_id列可能具有我想要的许多5字字符串。

我想获取所有特定站点ID。例如:e7089或e7459(我需要所有这些,第一个单词是随机的,例如e或t或etc,而四个数字是可变的)。

第一行是一个ticket_id和许多site_id s。我只需要以下网站ID,例如:G1231或G1236,而不需要括号中的地址:

ticket_idsite_id
sss sss-bb-12312312-12312G1231(AFSDGF-SDGSDGDG) B3213(ASDFSDF)

并这样做:

ticket_idsite_id
sss bb-12312312-12312G1231
SSS-BBG3212
-
2312-12312312-12312
G3212 12312B3213

我可以找到带有Regexp [AZ] \ d {1,4}的5字站点ID,但是我无法将其提取并插入新行。我的代码:

DROP TABLE IF EXISTS test2;

CREATE TABLE if NOT EXISTS test2 
(
    Ticket_id varchar,
    site_id varchar
);

INSERT INTO test2 
    SELECT ticket_id, site_id 
    FROM TEST 
    WHERE site_id regexp '[A-Z]\d{1,4}';

这将找到site_id的s并插入匹配的行。我不要那个。如何将第一个转换为第二个?

当前数据库:

Comart1Column2
Ticket1许多站点IDS
Ticket2许多站点ID

我想要的是:

Column1County2
Ticket1ID
Ticket1 ID Ticket1 ID Ticket1ID
Ticket1ID
Ticket1ID Ticket2 ID
Ticket2ID
Ticket2ID
Ticket2ID
  • 票证不需要任何更改,除了与他们的新行复制为新行分配的site_id
  • 每个机票都有多个site_id s,需要将需要分开为新行。
  • 需要在SQLite DB浏览器中完成(不幸的是没有Python)。

I want to search if a string exists in column2 (site_id) then put that string in a new table with the value of its before column in that row. The table has only 2 columns and the site_id column may have many 5-word strings that I want.

I want to get all of the the specific site id's. For example: E7089 or E7459 (I need all of them and the first word is random like E or T or etc and the four digits are variable).

The first row is with one ticket_id and many site_ids. I only need site ids like:g1231 or g1236 and not the addresses in parentheses:

ticket_idsite_id
sss-bb-12312312-12312g1231(afsdgf-sdgsdgdg), g1236(sdfsdgsdg), g3212(asdfas-dfsd), b2311(asdasd), b3213(asdfsdf)

And make it like this:

ticket_idsite_id
sss-bb-12312312-12312g1231
sss-bb-12312312-12312g3211
sss-bb-12312312-12312g1236
sss-bb-12312312-12312b2311
sss-bb-12312312-12312b3213

I can find the 5-word site id's with regexp [A-Z]\d{1,4}, but I can't extract and insert them into a new row. My code :

DROP TABLE IF EXISTS test2;

CREATE TABLE if NOT EXISTS test2 
(
    Ticket_id varchar,
    site_id varchar
);

INSERT INTO test2 
    SELECT ticket_id, site_id 
    FROM TEST 
    WHERE site_id regexp '[A-Z]\d{1,4}';

This will find the site_id's and insert rows that match. I don't want that. How to convert the first one to the second?

Current db :

column1column2
ticket1many site ids
ticket2many site ids

I want it to be :

column1column2
ticket1id
ticket1id
ticket1id
ticket1id
ticket2id
ticket2id
ticket2id
  • The tickets do not need any change except getting copied into new rows with their assigned site_id.
  • There are multiple site_ids for each ticket that need to be separated to new rows.
  • It needs to be done in SQLite db browser (unfortunately no Python).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

尐偏执 2025-02-03 00:00:21

您需要一个递归的CTE来拆分表表test1和substr()函数的列,以获取前5个字符以插入表test2 :

WITH cte AS (
    SELECT ticket_id, '' site_id, site_id || ',' s 
    FROM test1
    UNION ALL 
    SELECT ticket_id,
           SUBSTR(s, 0, INSTR(s, ',')),
           SUBSTR(s, INSTR(s, ',') + 1)
    FROM cte 
    WHERE s <> ''
) 
INSERT INTO test2 (ticket_id, site_id) 
SELECT ticket_id, SUBSTR(TRIM(site_id), 1, 5) 
FROM cte
WHERE site_id <> '';

请参阅

You need a recursive CTE to split the site_id column of the table test1 and SUBSTR() function to take the first 5 chars to insert in the table test2:

WITH cte AS (
    SELECT ticket_id, '' site_id, site_id || ',' s 
    FROM test1
    UNION ALL 
    SELECT ticket_id,
           SUBSTR(s, 0, INSTR(s, ',')),
           SUBSTR(s, INSTR(s, ',') + 1)
    FROM cte 
    WHERE s <> ''
) 
INSERT INTO test2 (ticket_id, site_id) 
SELECT ticket_id, SUBSTR(TRIM(site_id), 1, 5) 
FROM cte
WHERE site_id <> '';

See the demo.

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