我有一个带有NOT IN
子句的SQL查询,但它执行的时间太长。任何人都可以帮我替换以下查询中的NOT IN
。
Select
RIGHT(P.PARTNERDESCRIPTION, LEN(P.PARTNERDESCRIPTION) - CHARINDEX('-', P.PARTNERDESCRIPTION)) AS Channel,
C.CorporateId, C.corporatename,
MAX(convert(date,E.C3DeletionVerification))
from
employee E, Corporate C, Partners P
where
E.corporateid = C.corporateid
and C.partner = P.partnercode
and C.Corporateid not in (select CorporateId
from employee
where DeletionVerification is null
and CardNo IS NOT NULL
group by CorporateId)
group by
RIGHT(P.PARTNERDESCRIPTION, LEN(P.PARTNERDESCRIPTION) - CHARINDEX('-', P.PARTNERDESCRIPTION)),
C.CorporateId, C.corporatename
order by
MAX(convert(date, E.DeletionVerification))
答案 0 :(得分:1)
您可以将CTE用于查询并使用它,如下所示:
with emp as (select CorporateId
from employee
where DeletionVerification is null
and CardNo IS NOT NULL
group by CorporateId)
Select
RIGHT(P.PARTNERDESCRIPTION, LEN(P.PARTNERDESCRIPTION) - CHARINDEX('-', P.PARTNERDESCRIPTION)) AS Channel,
C.CorporateId, C.corporatename,
MAX(convert(date,E.C3DeletionVerification))
from
employee E, Corporate C, Partners P , emp ep
where
E.corporateid = C.corporateid
and C.partner = P.partnercode
and C.Corporateid <> ep.CorporateId
group by
RIGHT(P.PARTNERDESCRIPTION, LEN(P.PARTNERDESCRIPTION) - CHARINDEX('-', P.PARTNERDESCRIPTION)),
C.CorporateId, C.corporatename
order by
MAX(convert(date, E.DeletionVerification))
注意:未经测试。感谢
答案 1 :(得分:0)
请尝试稍微更改的此查询,但我希望它会更有效。
SELECT
RIGHT(P.PARTNERDESCRIPTION, LEN(P.PARTNERDESCRIPTION) - CHARINDEX('-', P.PARTNERDESCRIPTION)) AS Channel,
C.CorporateId, C.corporatename,
MAX(CONVERT(date,E.C3DeletionVerification))
FROM employee E
INNER JOIN Corporate C ON C.corporateid = E.corporateid
INNER JOIN Partners P ON P.partnercode = C.partner
WHERE NOT EXISTS(
SELECT DISTINCT CorporateId
FROM employee
WHERE CorporateId = c.Corporateid
AND DeletionVerification IS NULL
AND CardNo IS NOT NULL
)
GROUP BY RIGHT(P.PARTNERDESCRIPTION, LEN(P.PARTNERDESCRIPTION) - CHARINDEX('-', P.PARTNERDESCRIPTION)), C.CorporateId, C.corporatename
ORDER BY MAX(convert(date, E.DeletionVerification))