我有一個reviews表如下:
| r_id | 我的評論 |
|---|---|
| 1 | 帶有 TID 823 的箱子不能超過 40 公斤 |
| 2 | 標記為 tid 63157 的包裹重量不得超過 31 公斤 |
| 3 | TID 104124 和 TID 92341 的信封不能超過 94.477 公斤 |
| 4 | TID38204 不能超過 45.4 公斤,TID 8242602 不能超過 92 公斤 |
| 5 | 帶有 TID 94514 的箱子不能超過 52 公斤,也不能超過 51 公斤 |
我正在嘗試匹配兩件事。TID 和重量 (kg)。如您所見,有 3 件事要牢記
- 重量總是以kg并且不區分大小寫和在2種方式寫入的情況下,
kg與k.g和寫在2種方式<weight> <kg or k.g><weight><kg or k.g>(一個空間,一個沒有空間) - TID 不區分大小寫,可以用 2 種方式書寫
TID<id>或TID <id>(一種帶空格,一種不帶空格。 - 一些評論有多個 TID 和權重。我假設 TID 的第一次出現與權重的第一次出現相關,而 TID 的第二次出現是權重的第二次出現。我只增加了 2 個 TID/權重實體,但我希望它能夠動態地為任意數量的實體作業。
因此TID,如果評論只有 1 個權重和 1 個 TID,我可以提取和 權重。但是,如果它有多個,我就不會這樣做。所以我想將多個分成不同的行。
這是我想要的輸出
| r_id | 時間 | 重量 | 我的評論 |
|---|---|---|---|
| 1 | 823 | 40 | 帶有 TID 823 的箱子不能超過 40 公斤 |
| 2 | 63157 | 31 | 標記為 tid 63157 的包裹重量不得超過 31 公斤 |
| 3 | 104124 | 94.477 | TID 104124 和 TID 92341 的信封不能超過 94.477kg |
| 3 | 92341 | TID 104124 和 TID 92341 的信封不能超過 94.477kg | |
| 4 | 38204 | 45.4 | TID38204 不能超過 45.4 公斤,TID 8242602 不能超過 92 公斤 |
| 4 | 8242602 | 92 | TID38204 不能超過 45.4 公斤,TID 8242602 不能超過 92 公斤 |
| 5 | 94514 | 52 | 帶有 TID 94514 的箱子不能超過 52 公斤,也不能超過 51 公斤 |
| 5 | 51 | 帶有 TID 94514 的箱子不能超過 52 公斤,也不能超過 51 公斤 |
用于創建表/虛擬資料的 SQL:
CREATE TABLE reviews(
r_id number(3) NOT NULL,
my_comment VARCHAR(255) NOT NULL
);
INSERT INTO reviews (r_id, my_comment) VALUES (1, 'Boxes with the TID 823 cannot exceed 40 kg');
INSERT INTO reviews (r_id, my_comment) VALUES (2, 'Parcel with the marking tid 63157 must not make the weight go over 31 k.g');
INSERT INTO reviews (r_id, my_comment) VALUES (3, 'Envelopes with TID 104124 and TID 92341 cant excel above 94.477kg');
INSERT INTO reviews (r_id, my_comment) VALUES (4, 'TID38204 cannot go over 45.4 kg and TID 8242602 cannot go over 92kg');
INSERT INTO reviews (r_id, my_comment) VALUES (5, 'Box with the TID 94514 cannot go over 52kg but also cannot go over 51KG');
在我的嘗試中,我能夠提取 tid 和重量,但只能提取第一個實體,無法將其拆分為行。
SELECT
r_id,
REGEXP_SUBSTR (
REGEXP_SUBSTR (my_comment, '(tid).*?[0-9] ', 1, 1, 'i'),
'[0-9] '
) as "tid",
REGEXP_SUBSTR (
REGEXP_SUBSTR (my_comment, '(cannot exceed|go over| excel above).*?[0-9] ?(kg|k.g)', 1, 1, 'i'),
'[0-9] '
) as "weight"
FROM reviews;
uj5u.com熱心網友回復:
我能夠提取 tid 和重量,但只能提取第一個實體,無法將其拆分為行。
您的查詢,修改:
- 我對你已經寫的內容沒有做太多,因為你似乎對提取
tid和weight- 我所做的改變,是
regexp_substr的occurrence引數(是1,現在是column_value)
- 我所做的改變,是
- 為了獲得拆分資料,
cross join添加了“回圈”my_comment與tid和kg(以任何形式) 之間出現次數最多的次數- 例如,如果有 2
tid和 1kg,它將“回圈”2次 - 如果您只使用
connect by level子句,它也用于避免重復
- 例如,如果有 2
您確實將問題標記為 Oracle 10;我沒有它了,但我知道它不支持regexp_count功能。如果情況確實如此(您從未回答過 Koen 的問題),那么它將不起作用,您必須使用其他方式計算tid/weight出現次數。不過,我希望你不是在 10g 上。
我在 SQL*Plus 中運行了這段代碼。BREAK在這里只是為了很好地區分r_id和my_comment價值觀,沒有任何其他目的。
SQL> break on r_id on my_comment
SQL> SELECT r_id,
2 my_comment,
3 REGEXP_SUBSTR (REGEXP_SUBSTR (my_comment,
4 '(tid).*?[0-9] ',
5 1,
6 COLUMN_VALUE,
7 'i'),
8 '[0-9] ') AS "tid",
9 REGEXP_SUBSTR (
10 REGEXP_SUBSTR (
11 my_comment,
12 '(cannot exceed|go over| excel above).*?[0-9] ?(kg|k.g)',
13 1,
14 COLUMN_VALUE,
15 'i'),
16 '[0-9] ') AS "weight"
17 FROM reviews
18 CROSS JOIN
19 TABLE (
20 CAST (
21 MULTISET (
22 SELECT LEVEL
23 FROM DUAL
24 CONNECT BY LEVEL <= GREATEST (REGEXP_COUNT (my_comment, 'tid' , 1, 'i'),
25 REGEXP_COUNT (my_comment, '(kg|k.g)', 1, 'i')))
26 AS SYS.odcinumberlist));
這導致
R_ID MY_COMMENT tid weight
----- ------------------------------------------------------------------------- ------- -------
1 Boxes with the TID 823 cannot exceed 40 kg 823 40
2 Parcel with the marking tid 63157 must not make the weight go over 31 k.g 63157 31
3 Envelopes with TID 104124 and TID 92341 cant excel above 94.477kg 104124 94
92341
4 TID38204 cannot go over 45.4 kg and TID 8242602 cannot go over 92kg 38204 45
8242602 92
5 Box with the TID 94514 cannot go over 52kg but also cannot go over 51KG 94514 52
51
8 rows selected.
SQL>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/340638.html
