Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

SQL Parent / Child recursive query

$
0
0

Hello Spiceheads!

I need to generate a report using data from a database and the way i want it to be is a parent / child hierarchy. Basically i have 3 columns i would like to get

The "Parent_Sec" field is a String field that indicates the parent and it should match the "Security_Description" field. So"Security_Description" is the parent of "Parent_Sec" if i can say it like that. I did a lot of research and i finally managed to get this CTE query :
SQL
withcteReports(Security_Description,Parent_Sec,YTM_Sprd_Chge,steps)AS(SELECTSecurity_Description,Parent_Sec,YTM_Sprd_Chge,0FROMDSTSAutoSaves_SpreadswhereParent_Secisnullunionallselecte.Security_Description,e.Parent_Sec,e.YTM_Sprd_Chge,r.steps+1fromDSTSAutoSaves_SpreadseinnerjoincteReportsrone.Parent_Sec=r.Security_Description)selectDISTINCT...

Viewing all articles
Browse latest Browse all 4871

Trending Articles



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