recursive Sum

0

Please login or register to vote for this query.

(click on this box to dismiss)

http://stackoverflow.com/questions/11408878/recursive-sum-sql-server

Joomla Meta

Q&A about the site for Joomla! administrators, users, developers and designers

declare @t TABLE
(
  ID INT,
  Name VARCHAR(255),
  ParentID int,
  Value INT
)

INSERT INTO @t VALUES(1, 'Fred',NULL,1),
(2, 'Bets', NULL, 1),
(3, 'Wynand', 1, 21),
(4, 'Dewald', 1, 27),
(5, 'Katelynn', 3, 1),
(6, 'Jacques', 2,28),
(7, 'Luan', 6, 4),
(8, 'Ruben', 6, 2);


;WITH Personal AS
(
  SELECT N=1, ID,Name,ParentID,Value
  FROM @t
  WHERE ParentID IS NULL
  UNION ALL
  SELECT N+1, Av.ID,Av.Name,Av.ParentID,Av.Value
  FROM @t Av
  INNER JOIN  Personal P ON P.ID = Av.ParentID
)
SELECT * FROM Personal;

Enter Parameters

Options:
Switch to main site
loading Hold tight while we fetch your results