I have a list of products and components, that have different prices. Here is a simplified example.
drop table if exists #group_test
Create Table #group_test
(
ID Integer
, Hierarchy Nvarchar(200)
, Price Integer
)
Insert Into #group_test
values
(1,'001',10)
, (2,'001.001',20)
, (3,'001.002',5)
, (4,'001.002.001',3)
, (5,'001.002.002',2)
, (6,'001.002.003',4)
, (7,'001.003',6)
ID | Hierarchy | Price |
---|---|---|
1 | 001 | 10 |
2 | 001.001 | 20 |
3 | 001.002 | 5 |
4 | 001.002.001 | 3 |
5 | 001.002.002 | 2 |
6 | 001.002.003 | 4 |
7 | 001.003 | 6 |
The ID column is the ID of the product or component. The hierarchy shows what you need to build a product. For example product 1 consists of:
- Component 2
- Product 3
- Component 7
Product 3 consists of:
- Component 4
- Component 5
- Component 6
Now I want to sum the price of every product or component and all its subentities. This is my desired result:
ID | Hierarchy | Price | Total_Price |
---|---|---|---|
1 | 001 | 10 | 50 |
2 | 001.001 | 20 | 20 |
3 | 001.002 | 5 | 14 |
4 | 001.002.001 | 3 | 3 |
5 | 001.002.002 | 2 | 2 |
6 | 001.002.003 | 4 | 4 |
7 | 001.003 | 6 | 6 |
I achieved the desired result with a subquery. But since the table is very big I have the feeling that this is very inefficient.
Select
ID
, Hierarchy
, Price
, (
Select sum(Price)
From #group_test as in_tb
where in_tb.Hierarchy like Left(out_tb.Hierarchy, 3+4*(Len(out_tb.Hierarchy)-Len(Replace(out_tb.Hierarchy,'.','')))) + '%'
) as Total_Price
From #group_test as out_tb
I wanted to use an over (partition by) but that didn't work:
Select
ID
, Hierarchy
, Price
, sum(Price)
over (partition by Left
(
Hierarchy
, 3+4*
(
Len(Hierarchy)-
Len(Replace(Hierarchy,'.',''))
)
)
)
as Total_Price
From #group_test
Is there a way how I can use over (partition by) to achieve the same result as with my subquery? Or do you know any other methods that are efficient and easy to read?