Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VFP Update query with hierarchic relationship
Message
From
17/12/2019 06:08:49
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
VFP Update query with hierarchic relationship
Miscellaneous
Thread ID:
01672278
Message ID:
01672278
Views:
65
I wonder if this can be done. I have a hierarchy, which needs to update calculated values to their parents.
Level | ID | Parent_ID | Percent
1          100         0               0
2          101         100            0
3          102         101            0
2          103         100            0
1          104         0               0
Now I update percent of id 102:
lnID = 102
lnParent_Id = 101
Update Levels Set Percent = 10 WHERE id = m.lnID
Now I need to recalculate the parent's percentages:
UPDATE Levels ;
    SET Percent = (SELECT SUM(NVL(Percent,0)) / COUNT(*) ;
        FROM Levels Tmp ;
        WITH (BUFFERING = .T.) ;
        WHERE Tmp.Parent_ID = m.lnParent_Id) ;
    WHERE Levels.Id = m.lnParent_Id
So the result is:
Level | ID | Parent_ID | Percent
1          100         0                0
2          101         100            10
3          102         101            10
2          103         100            0
1          104         0                0
What is missing is the update for the grandparents and further (right now there is no limitation), which should then result in the following values:
Level | ID | Parent_ID | Percent
1          100         0                5
2          101         100            10
3          102         101            10
2          103         100            0
1          104         0                0
I wonder if that can be done without manually looping through the table?
Christian Isberner
Software Consultant
Next
Reply
Map
View

Click here to load this message in the networking platform