voodoochicken Posted March 9, 2008 Share Posted March 9, 2008 hi, a little background: i am trying to do something with trees. i am trying to update the number of children that hang from each node, i am also interested in knowing the total number of children of the 4th level relative to each node (with parent as 1st level). i am doing tree operations by levels rather than the usual 'preorder-whatever' to do less random accesses. lets assume that each node knows its absolute level already1. i am trying to set how many 'new children' from a certain level should be added to each node of the previous level, and which level did they come from. i am trying to do it with this (-some names changed- please check syntax, which is the idea of this topic):SELECT COD_PARENT, SUM(CONT1) AS CONT1, SUM(CONT2) AS CONT2, SUM(CONT3) AS CONT3, SUM(CONT4) AS CONT4, SUM(CONT5) AS CONT5 INTO TEMP_CONT FROM ALL_TREE WHERE LEVEL = ".$max_level." GROUP BY COD_PARENTif i did this right, at the end should be a table (temp_cont) with the sum of all new nodes that come from previous levels (all new nodes had originally a '1' in cont1, and each 'cont' should mean where did those nodes -or which previous level- come from, relative to this level)now i want to add each of those sums to the respective node. i am trying to do it with a sentence similar to this one (please check syntax, i am more uncertain about this one, again, some names changed):UPDATE ALL_TREE SET CONT2=TEMP_CONT.CONT1, CONT3=TEMP_CONT.CONT2, CONT4=TEMP_CONT.CONT3, CONT5=CONT5+TEMP_CONT.CONT4, TOTAL_CHILDREN = (TOTAL_CHILDREN + TEMP_CONT.CONT1 + TEMP_CONT.CONT2 + TEMP_CONT.CONT3 + TEMP_CONT.CONT4 + TEMP_CONT.CONT5), TOTAL_LEVEL4 = (TOTAL_LEVEL4+TEMP_CONT.CONT3) WHERE COD = TEMP_CONT.COD_PARENTmore or less. i am not sure about the joint part, where i add the sums to the respective counters, where i relate one table with the other. the idea would be to try it, but first i have to do lots of things before having a test table, so i want to check syntax before testing ittnx Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2008 Share Posted March 10, 2008 The syntax looks fine. You can also check SQL syntax without running the query in most SQL tools. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.