Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How aggregate the data of an Amount field at different levels to a result set generated by recursive CTE

$
0
0


we are using  Sql server 2005.i have generated a result set using a recursive CTE with Multiple level based on the Child and parent relation ship.<o:p></o:p>

The Below image is the Result set that i had ended up with.<o:p></o:p>


this is how the CTE works.<o:p></o:p>

It takes one LE_ID(child) and when LE_ID(Child) = Ultimate_id(grand parent), Its level 1. and it then takes LE_ID(CHILD) for the LVL 1  and inner joins with the field (PRNT_ID) in the base table for that ULTIMATE_ID, and for all the matches found it  names it  as LVL 2. and then  all the LVL 2 LE_ID's are inner joined with PRNT_ID field of the base table for that particular (PRNT_ID) and termed as LVL 3 and so on... . <o:p></o:p>

and the  other part of requirement says the 'Total' field should be populated on the Below criteria. <o:p></o:p>

 

For LVL = 1 the sum of all the LE_ID for that particular ULTIMATE_ID should be populated in the 'Total' field.<o:p></o:p>

From LVL = 2 on it should take an LE_ID and find the same record in  PRNT_ID FIeld  and then take IT's LE_ID and find any record with that PRNT_ID.. So on.. and ADD up the BNK_AMT and populate the 'Total' field.<o:p></o:p>

Example: ULTIMATE_ID = 213<o:p></o:p>

LE_ID In LVL 2 are 214, 215, 216 there is match for 214 in LVL 3, which is has LE_ID has 300 and this has not as PRNT_ID number so 17000.00+30.00 = 17030.00 should be populated as 'Total' for that records. <o:p></o:p>

 

Please need an advice on this. Thanks.<o:p></o:p>


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>