Current table looks like this:
Each object has a unique name and it's parent name and a hierarchy.
name parent hierarchy value
A A 1 100
B A 2 0.5
C B 3 0.5
My expected result is:
name parent hierarchy value
A A 1 100
B A 2 50
C B 3 25
My query is
SELECT tb1.[name],
tb1.[parent],
tb1.[hierarchy],
CASE
WHEN tb1.[hierarchy]=1
THEN tb1.[value]
WHEN tb1.[hierarchy]=2
THEN tb1.[value]*tb2.[value]
AS [value]
FROM
table tb1
INNER JOIN
table tb2
ON
tb1.[parent]= tb2.[name]
This code works only if we have hierarchy 2. How to modify it to work for any number of hierarchy values.
EDIT:
What if it had an id along with which we can identify the data uniquely?
id name parent hierarchy value
1 A A 1 100
1 B A 2 50
1 C B 3 25
2 A A 1 30
2 D A 2 0.1