Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
akwok
Starting Member
10 Posts |
Posted - 2006-10-19 : 02:08:11
|
| Hi,I need help to do some thing in MS SQL.something equivalent to CONNECT BY PRIOR IN ORACLEThe columns are:ID, PARENT_ID, MONEY1 0 52 1 33 1 14 2 35 4 2I want to make a count of MONEY that the whole tree of 2 hasit will be money of 2 + money of 4 + money of 5How do we write such scriptIf i want to avoid using store procedures, is there anyways to do it without?Thank you in advance!! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-19 : 02:59:37
|
Yes there is, but only if you know the meximum allowed depth for the tree.DECLARE @Tree TABLE (ID INT, PARENT_ID INT, [MONEY] INT)INSERT @TreeSELECT 1, 0, 5 UNION ALLSELECT 2, 1, 3 UNION ALLSELECT 3, 1, 1 UNION ALLSELECT 4, 2, 3 UNION ALLSELECT 5, 4, 2DECLARE @Node INTSELECT @Node = 2SELECT SUM(CASE WHEN pID = @Node THEN ISNULL(pMoney, 0) + ISNULL(c1_Money, 0) + ISNULL(c2_Money, 0) + ISNULL(c3_Money, 0) WHEN c1_ID = @Node THEN ISNULL(c1_Money, 0) + ISNULL(c2_Money, 0) + ISNULL(c3_Money, 0) WHEN c2_ID = @Node THEN ISNULL(c2_Money, 0) + ISNULL(c3_Money, 0) WHEN c3_ID = @Node THEN ISNULL(c3_Money, 0) ELSE 0 END)FROM ( SELECT p.id pID, p.[Money] pMoney, c1.id c1_ID, c1.[Money] c1_Money, c2.id c2_ID, c2.[Money] c2_Money, c3.id c3_ID, c3.[Money] c3_Money FROM ( SELECT ID, [Money] FROM @Tree WHERE Parent_ID = 0 ) p LEFT JOIN @Tree c1 ON c1.Parent_ID = p.ID LEFT JOIN @Tree c2 ON c2.Parent_ID = c1.ID LEFT JOIN @Tree c3 ON c3.Parent_ID = c2.ID ) x Peter LarssonHelsingborg, Sweden |
 |
|
|
akwok
Starting Member
10 Posts |
Posted - 2006-10-19 : 03:21:30
|
| What is the way to do when I find the level ?or with stored procedures?because i shouldn't know how many levels i have |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-19 : 03:25:12
|
| Make a stored procedure.Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-19 : 03:33:49
|
Something like this.DECLARE @Tree TABLE (ID INT, PARENT_ID INT, [MONEY] INT)INSERT @TreeSELECT 1, 0, 5 UNION ALLSELECT 2, 1, 3 UNION ALLSELECT 3, 1, 1 UNION ALLSELECT 4, 2, 3 UNION ALLSELECT 5, 4, 2DECLARE @Iter TABLE (Generation INT, ID INT, [Money] INT)INSERT @IterSELECT 0, t.ID, t.[Money]FROM @Tree tWHERE t.ID = 2WHILE @@ROWCOUNT > 0 INSERT @Iter SELECT 1 + (SELECT MAX(x.Generation) FROM @Iter x), t.ID, t.[Money] FROM @Tree t WHERE t.Parent_ID IN (SELECT s.ID FROM @Iter s WHERE s.Generation = (SELECT MAX(q.Generation) FROM @Iter q))SELECT SUM([Money])FROM @Iter Peter LarssonHelsingborg, Sweden |
 |
|
|
akwok
Starting Member
10 Posts |
Posted - 2006-10-19 : 04:58:33
|
| thanks for the reply. What If i want to add a new column, and insert the sum of money of your tree for every record in the table.as result, 1 will have 14, 2 will have 8 , 3 will have 1, 4 will have5 and 5 will have 2.Thanks again for the quick reply. |
 |
|
|
akwok
Starting Member
10 Posts |
Posted - 2006-10-19 : 05:01:42
|
| I know that there's a method to first find out on which level each record is. then do something with the level... and add up all the money. it's like a recursive thing. i have done it once in the past, but lost the code. sigh... |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-19 : 05:07:14
|
How hard can it be?When you are given a suggestion or solution for your problem, take same time and try to understand what is going on in the code.For your latest problem, I just changed the LAST select statement asSELECT SUM([Money])FROM @IterSELECT i.ID, i.[Money], (SELECT SUM(z.[Money]) FROM @Iter z WHERE z.Generation >= i.Generation) AllMoneyFROM @Iter iORDER BY i.Generation Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-19 : 05:13:38
|
Also changeINSERT @IterSELECT 0, t.ID, t.[Money]FROM @Tree tWHERE t.ID = 2WHERE t.ID = 1 Even better, make this a STORED PROCEDURE and make the number after the equal sign as a parameter to the SP.Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-19 : 05:24:33
|
| [code]CREATE PROCEDURE dbo.uspGetMoneyTree( @TopNode INT, @AllNodes BIT = 1)ASSET NOCOUNT ONDECLARE @Iter TABLE (Generation INT, ID INT, [Money] INT)INSERT @IterSELECT 0, t.ID, t.[Money]FROM MyTreeTable tWHERE t.ID = @TopNodeWHILE @@ROWCOUNT > 0 INSERT @Iter SELECT 1 + (SELECT MAX(x.Generation) FROM @Iter x), t.ID, t.[Money] FROM @Tree t WHERE t.Parent_ID IN (SELECT s.ID FROM @Iter s WHERE s.Generation = (SELECT MAX(q.Generation) FROM @Iter q))SELECT i.ID, i.[Money], (SELECT SUM(z.[Money]) FROM @Iter z WHERE z.Generation >= i.Generation) AllMoneyFROM @Iter iWHERE i.ID = @TopNode OR @AllNodes = 1ORDER BY i.Generation[/code]Call with eitheruspGetMoneyTree 2uspGetMoneyTree 1, 0uspGetMoneyTree 2, 1Peter LarssonHelsingborg, Sweden |
 |
|
|
akwok
Starting Member
10 Posts |
Posted - 2006-10-19 : 05:49:57
|
| something i don't understand with your method is that, it's like a pyramid, if you are in the 2nd generation, even a record is not reporting to you, you still add his money to his account.take record 3 for example. we shouldn't count the money of 4 and 5 to him as 4 and 5 are not reporting to him, but reporting to 2 and 4.But really. i couldn't ask more, thanks for your help.!! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-19 : 05:57:51
|
You don't. I just tried uspMoneyBack 3 and got this resultID Money AllMoney-- ----- -------- 3 1 1 Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|