実行に約3分かかるクエリがあります。クエリはレポートに使用されており、より効率的にしたいと思います。ボトルネックは内側の結合だと思います。あなたたちが解決策を持っているかどうかを知りたいだけで間違っている可能性があります。
SELECT DISTINCT
SUBSTRING(T1.DateTime, 1, 4) AS Year,
SUBSTRING(T1.DateTime, 5, 2) AS Month,
SUBSTRING(T1.DateTime, 7, 2) AS Day,
T1.PipeNr, T1.SalesOrder, T1.JobNr, SIST.DefectCode AS DefectRef,
DEFCODES.DefectCode, DEFCODES.DefectDesc, SIST.ODYes, SIST.LocationWrap,
T1.OWS0601, T1.OWS0602, T1.OWS0603, T1.CrossWeld
FROM PIPEDB.dbo.SIST INNER JOIN PIPEDB.dbo.MPIPEID T1 ON PIPEDB.dbo.SIST.PipeNr = T1.PipeNr INNER JOIN
PIPEDB.dbo.DEFCODES ON PIPEDB.dbo.SIST.DefectCode = PIPEDB.dbo.DEFCODES.DefectRef
WHERE PIPEDB.dbo.SIST.DefectCode
IN (
SELECT Top (10) PIPEDB.dbo.SIST.DefectCode
FROM PIPEDB.dbo.SIST INNER JOIN PIPEDB.dbo.MPIPEID T2 ON PIPEDB.dbo.SIST.PipeNr = T2.PipeNr INNER JOIN
PIPEDB.dbo.DEFCODES ON PIPEDB.dbo.SIST.DefectCode = PIPEDB.dbo.DEFCODES.DefectRef
WHERE SUBSTRING(T2.DateTime, 1, 4) = SUBSTRING(T1.DateTime, 1, 4) AND SUBSTRING(T2.DateTime, 5, 2) = SUBSTRING(T1.DateTime, 5, 2) AND
SUBSTRING(T2.DateTime, 7, 2) = SUBSTRING(T1.DateTime, 7, 2)
GROUP BY PIPEDB.dbo.SIST.DefectCode
ORDER BY COUNT(PIPEDB.dbo.SIST.PipeNr) DESC)
AND (PIPEDB.dbo.DEFCODES.DefectDesc IN ("Cut To Remove Defect")) AND
((CASE WHEN T1.CrossWeld = 1 THEN 1 WHEN T1.CrossWeld = 0 THEN 2 END) = @Crossweld OR @Crossweld = 0)