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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 accumulated calculation in sql server

Author  Topic 

mrsqlserver
Starting Member

6 Posts

Posted - 2014-07-09 : 01:17:25
I want to do this query on sql server

I can do this with loop but would like to know if there is any easier way that has better performance

I have table for referrals , scenario 1 : if A has 5 points and refer B that has 7 then the query should show 12 pts for A (A points + B points) scenario 2 : if A has 5 and refer to B that has 7 and A refer to C that has 3 points and B refer to D that has 4 pts and so on.. in this case A takes all points of people A + B + C + D

my table look like this

Refs

sID bigint

sName varchar(50)

sPoints int

sRefID bigint

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-07-09 : 06:55:13
You will need a recursive CTE. There are many examples here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=98595



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -