直接贴代码吧= =
WITH CTE AS ( -->Begin 一个定位点成员 SELECT COUNTRYORDERID,HSNAME, COUNTRYNAME,PARENTORDERID, IMPORTCURRENTSUMAMOUNT, IMPORTCONTRASTSUMAMOUNTPERCENTAGE, IMPORTCURRENTSUMAMOUNTPERCENTAGE, CAST(COUNTRYNAME AS NVARCHAR(max)) AS TE,0 AS Levle FROM #TempTableDetail WHERE PARENTORDERID IS NULL AND HSNAME IN (SELECT HSNAME FROM #TempTableData) -->End UNION ALL -->Begin一个递归成员 SELECT #TempTableDetail.COUNTRYORDERID, #TempTableDetail.HSNAME, #TempTableDetail.COUNTRYNAME,#TempTableDetail.PARENTORDERID, #TempTableDetail.IMPORTCURRENTSUMAMOUNT, #TempTableDetail.IMPORTCONTRASTSUMAMOUNTPERCENTAGE, #TempTableDetail.IMPORTCURRENTSUMAMOUNTPERCENTAGE, CAST(REPLICATE(‘ ‘,LEN(CTE.TE))+#TempTableDetail.COUNTRYNAME AS NVARCHAR(MAX)) AS TE,Levle+1 AS Levle FROM #TempTableDetail INNER JOIN CTE ON #TempTableDetail.PARENTORDERID=CTE.COUNTRYORDERID --group by #TempTableDetail.HSNAME WHERE #TempTableDetail.HSNAME IN (SELECT HSNAME FROM #TempTableData) -->End ) INSERT INTO #TempResult(COUNTRYORDERID,HSNAME,COUNTRYNAME,IMPORTCONTRASTSUMAMOUNTPERCENTAGE,IMPORTCURRENTSUMAMOUNTPERCENTAGE,IMPORTCURRENTSUMAMOUNT) SELECT DISTINCT COUNTRYORDERID, HSNAME,TE,IMPORTCONTRASTSUMAMOUNTPERCENTAGE, IMPORTCURRENTSUMAMOUNTPERCENTAGE,IMPORTCURRENTSUMAMOUNT FROM CTE ORDER BY HSNAME,COUNTRYORDERID;
效果:
时间: 2024-10-17 22:11:47