Skip to content
Advertisement

Recursively calculate product weights in product tree

I’m developing a software for Consumer price index calculation and one step of all calculation is to calculate product weights in product category. This should be done in SQL server side. I have MS SQL Server 2008. The product catalog are stored in a tree:

It is simplified structure of my table, so i need to calculate weights for categories.

For example:

  • weight for category ‘01.1.1’ = sum of product weight 01.1.1.101 + 01.1.1.102 + 01.1.1.103 + 01.1.1.104 =0.51
  • for category ‘01.1.2’ = sum of 01.1.2.201 + 01.1.2.201 = 0.35
  • for category 01.1 = sum of 01.1.1 +01.1.2 =0.51 +0.35 = 0.86
  • for category 01 = sum of 01.1 + 01.2 (not showed in my example)

And my problem actually is, I can’t calculate weights of parent categories.

Maybe someone can help me to solve my problem? Thank you!

Advertisement

Answer

I think Joakim’s answer does not give the correct result.

Perhaps this will work:

Result:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement