I have a reviews table as follows:
| r_id | my_comment |
|---|---|
| 1 | Boxes with the TID 823 cannot exceed 40 kg |
| 2 | Parcel with the marking tid 63157 must not make the weight go over 31 k.g |
| 3 | Envelopes with TID 104124 and TID 92341 cant excel above 94.477kg |
| 4 | TID38204 cannot go over 45.4 kg and TID 8242602 cannot go over 92kg |
| 5 | Box with the TID 94514 cannot go over 52kg but also cannot go over 51KG |
I am trying to match 2 things. The TID and the weight (kg). As you can see there are a 3 things to keep in mind
- Weight is always in kg and is written case insensitively and in 2 ways,
kgandk.gand written in 2 ways<weight> <kg or k.g><weight><kg or k.g>(one with space, one without space) - TID is written case insensitively and can be written in 2 ways
TID<id>orTID <id>(one with a space, one without a space. - Some comments have multiple TID and weights. I am making the assumption that the first appearance of the TID is tied to the first appearance of the weight and the the second appearance of the TID is for the second appearance of the weight. I've only gone up to 2 instances of the TID/weight but I would like it to dynamically work for any amount of instances.
So I am able to extract the TID and the weight if the comment only has 1 weight and 1 TID. However, if it has multiple, I fail to do so. So I want to separate the multiple into different rows.
This is my desired output
| r_id | tid | weight | my_comment |
|---|---|---|---|
| 1 | 823 | 40 | Boxes with the TID 823 cannot exceed 40 kg |
| 2 | 63157 | 31 | Parcel with the marking tid 63157 must not make the weight go over 31 k.g |
| 3 | 104124 | 94.477 | Envelopes with TID 104124 and TID 92341 can't excel above 94.477kg |
| 3 | 92341 | Envelopes with TID 104124 and TID 92341 can't excel above 94.477kg | |
| 4 | 38204 | 45.4 | TID38204 cannot go over 45.4 kg and TID 8242602 cannot go over 92kg |
| 4 | 8242602 | 92 | TID38204 cannot go over 45.4 kg and TID 8242602 cannot go over 92kg |
| 5 | 94514 | 52 | Box with the TID 94514 cannot go over 52kg but also cannot go over 51KG |
| 5 | 51 | Box with the TID 94514 cannot go over 52kg but also cannot go over 51KG |
SQL to create table/dummy data:
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');
In my attempt, I am able to extract the tid and weight, but only the first instance and not able to split it into rows.
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;