CREATE VIEW v_orgtype99 as
SELECT t1.SYS_ORG_TYPE_NAME top_name1,
t2.SYS_ORG_TYPE_NAME top_name2,
--若机构表第二层为空,算第一层的机构编码,依次类推。
CASE WHEN o2.SYS_ORG_ID IS NULL THEN o1.SYS_ORG_FINANCIAL_CODE
WHEN o3.SYS_ORG_ID IS NULL THEN o2.SYS_ORG_FINANCIAL_CODE
WHEN o4.SYS_ORG_ID IS NULL THEN o3.SYS_ORG_FINANCIAL_CODE
ELSE o4.SYS_ORG_FINANCIAL_CODE END SYS_ORG_FINANCIAL_CODE,
--若机构表第二层为空,算第一层的机构名,依次类推。
CASE WHEN o2.SYS_ORG_ID IS NULL THEN o1.SYS_ORG_NAME
WHEN o3.SYS_ORG_ID IS NULL THEN o2.SYS_ORG_NAME
WHEN o4.SYS_ORG_ID IS NULL THEN o3.SYS_ORG_NAME
ELSE o4.SYS_ORG_NAME END SYS_ORG_NAME,
t3.SYS_ORG_TYPE_ID one_level_code,t3.SYS_ORG_TYPE_NAME one_level_name, --机构类型表最底层
o1.SYS_ORG_ID two_level_code,o1.SYS_ORG_NAME two_level_name, --机构表第一层
o2.SYS_ORG_ID three_level_code,o2.SYS_ORG_NAME three_level_name, --机构表第二层
o3.SYS_ORG_ID four_level_code,o3.SYS_ORG_NAME four_level_name, --机构表第三层
o4.SYS_ORG_ID five_level_code,o4.SYS_ORG_NAME five_level_name --机构表第四层
FROM
SYS_ORG_TYPE_TB t1
INNER JOIN SYS_ORG_TYPE_TB t2 ON t1.SYS_ORG_TYPE_ID = t2.SYS_ORG_TYPE_UPID --机构类型第一层t1,第二层t2
LEFT JOIN SYS_ORG_TYPE_TB t3 ON t2.SYS_ORG_TYPE_ID = t3.SYS_ORG_TYPE_UPID --机构类型第三层t3
LEFT JOIN (SELECT * FROM SYS_ORG_TB WHERE SYS_ORG_UPID IS NULL) o1
ON t3.SYS_ORG_TYPE_ID = o1.SYS_ORG_TYPE_ID --机构表第一层o1
LEFT JOIN SYS_ORG_TB o2 ON o1.SYS_ORG_ID = o2.SYS_ORG_UPID --机构表第二层o2
LEFT JOIN SYS_ORG_TB o3 ON o2.SYS_ORG_ID = o3.SYS_ORG_UPID --机构表第三层o3
LEFT JOIN SYS_ORG_TB o4 ON o3.SYS_ORG_ID = o4.SYS_ORG_UPID --机构表第四层o4
WHERE t1.SYS_ORG_TYPE_NAME='商业银行'