新建表
DROP TABLE Industry; CREATE TABLE Industry( IndustryCode VARCHAR(40),IndustryName VARCHAR(100),ParentID VARCHAR(40) )
数据截图:
现在数据是这个样子的,我们需要找出对应的层级关系,例如:
实现SQL如下:
SELECT A.IndustryCode AS CLASS01,A.INDUSTRYNAME CLASS_01NM, B.IndustryCode AS CLASS02,B.INDUSTRYNAME CLASS_02NM, C.IndustryCode AS CLASS03,C.INDUSTRYNAME CLASS_03NM, C.IndustryCode AS CLASS04,C.INDUSTRYNAME CLASS_04NM, DENSE_RANK() over(ORDER BY A.INDUSTRYCODE) SEQ FROM Industry A,Industry B,Industry C,Industry D WHERE A.INDUSTRYCODE=B.PARENTID AND B.INDUSTRYCODE=C.PARENTID AND C.INDUSTRYCODE=D.PARENTID