Create index view with self join
If all you need is to speed up your query, you can create the following indexed view:

Select ParentDetailID ,
       SUM(Quantity) as RemainQuantity,
       COUNT_BIG(*) as CountBig
From SAM.Detail 
Group By ParentDetailID 


You can join it you Detail table, and your query might run faster.