2つの質問があります。
First:
2つのクエリを1つに組み合わせて、レポート作成用のストアドプロシージャを配置しようとしています。彼らです
クエリ1
SELECT T0.Name AS Period, SUM(ISNULL(T2.LineTotal, 0)) AS CurrentDebtors, MAX(T1.DocRate) AS ExchangeRate,
SUM(CASE WHEN DATEDIFF(day, T1.DocDate, T1.DocDueDate) > 30
THEN T2.LineTotal END) AS NonCurrentDebtors
FROM OFPR T0 LEFT OUTER JOIN OINV T1 ON T0.AbsEntry = T1.FinncPriod
INNER JOIN INV1 T2 ON T1.DocEntry = T2.DocEntry WHERE YEAR(T1.DocDate) = @Year
GROUP BY T0.Name
ORDER BY T0.Name
クエリ2
SELECT T0.Name AS Period, SUM(T1.DocTotal) AS TurnoverMonth
FROM dbo.OFPR T0 LEFT OUTER JOIN dbo.ORCT T1 ON T0.AbsEntry = T1.FinncPriod
WHERE YEAR(T1.DocDate) = @Year
GROUP BY T0.Name
ORDER BY T0.Name
私はそれらをこれに組み合わせました:
SELECT T0.Name AS Period, SUM(ISNULL(T2.LineTotal, 0)) AS CurrentDebtors, MAX(T1.DocRate) AS ExchangeRate,
SUM(CASE WHEN DATEDIFF(day, T1.DocDate, T1.DocDueDate) > 30
THEN T2.LineTotal END) AS NonCurrentDebtors , SUM(ISNULL(T3.DocTotal, 0)) AS TurnoverMonth
FROM OFPR T0 LEFT OUTER JOIN OINV T1 ON T0.AbsEntry = T1.FinncPriod
INNER JOIN INV1 T2 ON T1.DocEntry = T2.DocEntry
JOIN ORCT T3 ON T0.AbsEntry = T3.FinncPriod
WHERE YEAR(T1.DocDate) = @Year
GROUP BY T0.Name
ORDER BY T0.Name
問題は、上記の2つの個別のクエリの結果は正しいものの、上記の結合されたクエリは非常に大量の誤った値を返すことです。2つを適切に組み合わせるにはどうすればよいですか?
Second:
クエリ1は、非常に大きなデータがある場合にも実行に時間がかかりますが、その効率を向上させる方法はありますか?Microsoft SQLServer2008の使用