它正在变化的子查询(ColumnNm = hierarchylvl#)的Where子句。让我知道任何提高查询性能的方法。 有没有办法重写子查询?加入?使用任何其他功能?
select * from
(select
(SELECT TOP 1 itmtxt FROM TM.Code WHERE Catcd = (select Catcd from TM.ClientHierarchy where OBJID='system' and TableNm='resume1' and ColumnNm ='hierarchylvl0' and delflg = 0) and delflg = 0 AND LngCd IN (0,-1) AND ItmCd = r1.hierarchylvl0) AS hierarchylvl0txt,
(SELECT TOP 1 itmtxt FROM TM.Code WHERE Catcd = (select Catcd from TM.ClientHierarchy where OBJID='system' and TableNm='resume1' and ColumnNm ='hierarchylvl1' and delflg = 0) and delflg = 0 and ItmCd=r1.HierarchyLvl1 AND LngCd IN (0,-1)) AS hierarchylvl1txt,
(SELECT TOP 1 itmtxt FROM TM.Code WHERE Catcd = (select Catcd from TM.ClientHierarchy where OBJID='system' and TableNm='resume1' and ColumnNm ='hierarchylvl2' and delflg = 0) AND lngcd IN (0,-1) and delflg = 0 and ItmCd=r1.HierarchyLvl2) AS hierarchylvl2txt,
(SELECT TOP 1 itmtxt FROM TM.Code WHERE Catcd = (select Catcd from TM.ClientHierarchy where OBJID='system' and TableNm='resume1' and ColumnNm ='hierarchylvl3' and delflg = 0) AND lngcd IN (0,-1) and delflg = 0 and ItmCd=r1.HierarchyLvl3) AS hierarchylvl3txt,
(SELECT TOP 1 itmtxt FROM TM.Code WHERE Catcd = (select Catcd from TM.ClientHierarchy where OBJID='system' and TableNm='resume1' and ColumnNm ='hierarchylvl4' and delflg = 0) AND lngcd IN (0,-1) and delflg = 0 and ItmCd=r1.HierarchyLvl4) AS hierarchylvl4txt,
(SELECT TOP 1 itmtxt FROM TM.Code WHERE Catcd = (select Catcd from TM.ClientHierarchy where OBJID='system' and TableNm='resume1' and ColumnNm ='hierarchylvl5' and delflg = 0) AND lngcd IN (0,-1) and delflg = 0 and ItmCd=r1.HierarchyLvl5) AS hierarchylvl5txt,
(SELECT TOP 1 itmtxt FROM TM.Code WHERE Catcd = (select Catcd from TM.ClientHierarchy where OBJID='system' and TableNm='resume1' and ColumnNm ='hierarchylvl6' and delflg = 0) AND lngcd IN (0,-1) and delflg = 0 and ItmCd=r1.HierarchyLvl6) AS hierarchylvl6txt,
(SELECT TOP 1 itmtxt FROM TM.Code WHERE Catcd = (select Catcd from TM.ClientHierarchy where OBJID='system' and TableNm='resume1' and ColumnNm ='hierarchylvl7' and delflg = 0) AND lngcd IN (0,-1) and delflg = 0 and ItmCd=r1.HierarchyLvl7) AS hierarchylvl7txt,
(SELECT TOP 1 itmtxt FROM TM.Code WHERE Catcd = (select Catcd from TM.ClientHierarchy where OBJID='system' and TableNm='resume1' and ColumnNm ='hierarchylvl8' and delflg = 0) AND lngcd IN (0,-1) and delflg = 0 and ItmCd=r1.HierarchyLvl8) AS hierarchylvl8txt,
(SELECT TOP 1 itmtxt FROM TM.Code WHERE Catcd = (select Catcd from TM.ClientHierarchy where OBJID='system' and TableNm='resume1' and ColumnNm ='hierarchylvl9' and delflg = 0) and delflg = 0 and ItmCd=r1.HierarchyLvl9 AND LngCd IN (0,-1)) AS hierarchylvl9txt,
sr.rsrcnum, sr.fllnm, isnull(mgr.fllnm,'') as mgrnm, d.actid, d.goaldesctxt ,
d.cmpltstscd,
(select TOP 1 itmtxt from tm.Code where catcd = '12103' and itmcd = d.cmpltstscd and lngcd = 0 and delflg = 0) as Status,
d.strtdt, d.enddt, dta
from
TM.matrixgoal d
inner join
TM.sresource sr on d.rsrcid=sr.rsrcid and sr.delflg=0 and sr.stscd<>1 and sr.rsrcid > 7
left join
TM.sresource mgr on mgr.rsrcid=sr.mgr1id and mgr.delflg=0 and mgr.stscd<>1
inner join
TM.resume1 r1 on r1.rsrcid=sr.rsrcid
答案 0 :(得分:0)
您的子查询部分可以使用IN
运算符
SELECT TOP 1
itmtxt
FROM TM.Code
WHERE Catcd IN (SELECT
Catcd
FROM TM.ClientHierarchy
WHERE OBJID = 'system'
AND TableNm = 'resume1'
AND ColumnNm IN ('hierarchylvl0', 'hierarchylvl1', 'hierarchylvl2', 'hierarchylvl3', 'hierarchylvl4', 'hierarchylvl5',
'hierarchylvl6', 'hierarchylvl7', 'hierarchylvl8', 'hierarchylvl9')
AND delflg = 0)
AND delflg = 0
AND LngCd IN (0, -1)