I have a table of category and has an attribute of
This tag_id is the sequence or pattern on what its parent or child is.
Sample:
CREATE TABLE category (
id int,
parent_id int,
code varchar(255),
name varchar(255).
tag_id varchar(255)
);
INSERT INTO category (id, parent_id, code, name, tag_id)
VALUES
(1,null,0,'category 1','|1|'),
(2,null,1,'category 2','|2|'),
(3,null,2,'category 3','|3|'),
(4,null,3,'category 4','|4|'),
(5,4,4,'category 5','|4|5|'),
(6,5,5,'category 6','|4|5|6|');
As you can see, you may determine if the category is parent, if the first value inside the tag_id is its id.
I want to group by this category by same first parent id
Reason: I'm joining this table to another table wherein there's an amount column and I want to sum and group by using the first parent id.
For example I have a sales table wherein it has a relationship with product table. And this product table has relationship with category table.
In sales table it has column there total_amount, and product_id. This product_id is linked to the id of the product table. And this product table has a category_id column that is linked to the id of the category table.
The main goal is to get the SUM OF TOTAL_AMOUNT that a first level category was sold. I just need how to split this | character and get only the first level category
Thank you!