在这里,我们在一个存储过程中加入多个 select 语句。在每个嵌套查询中,我们对 year 参数和其他一些参数进行过滤。我确信存在会损害性能的冗余,但不确定如何改进。任何想法?谢谢!
SELECT Months.MonthName,
GPSDailyAccruals.MonthNumber,
GPSDailyAccruals.YearNumber,
ISNULL(GPSDailyAccruals.Amount,0) AS GPSDailyAccrualAmount,
ISNULL(GPSMonthlyAccruals.Amount,0) AS GPSMonthlyAccrualAmount,
ISNULL(GPSMonthlyAccrualsReceivable.Amount,0) AS GPSMonthlyAccrualReceivableAmount,
ISNULL(GMIDailyAccruals.Amount,0) AS GMIDailyAccrualAmount,
ISNULL(GLPayable.AMOUNT,0) AS GLPayable,
+ISNULL(Paid.AMOUNT,0) AS Paid,
ISNULL(GMIDailyAccruals.Amount,0)-ISNULL(Paid.AMOUNT,0) AS GMIvsPAID,
ISNULL(AdjustmentsLateTradeAmount.Amount,0) AS AdjustmentsLateTradeAmount,
ISNULL(GMIDailyAccruals.Amount,0) + ISNULL(AdjustmentsLateTradeAmount.Amount,0) - ISNULL(GLPayable.AMOUNT,0) AS GLOOB,
CASE WHEN ISNULL(MatchedTrades.Amount,0) <> 0 AND ISNULL(TotalTrades.Amount,0) <> 0 THEN (CONVERT(numeric,MatchedTrades.Amount) / CONVERT(numeric,TotalTrades.Amount)) * 100 ELSE 0 END AS MatchingPercentage
FROM
(
SELECT Months.MonthNumber,
Months.MonthName
FROM Months
) Months
LEFT JOIN (
SELECT MONTH(TradeDate) AS MonthNumber,
YEAR(TradeDate) AS YearNumber,
SUM(CASE WHEN RecordType = 'C' THEN Fee ELSE Fee*-1 END) AS Amount
FROM vFutureCMEGPSPay
WHERE YEAR(TradeDate) = @year
AND StatusID <> 4
AND IsCancel = 'N'
GROUP BY MONTH(TradeDate), YEAR(TradeDate)
) GPSDailyAccruals
ON Months.MonthNumber = GPSDailyAccruals.MonthNumber
LEFT JOIN (
SELECT MonthNumber,
YearNumber,
SUM(CASE WHEN RecordType = 'C' THEN Fee ELSE Fee*-1 END) AS Amount
FROM TradeCMEGPSMonthly
WHERE YearNumber = @year
AND Expired_CarryoverIndicator NOT IN ('E','C')
GROUP BY MonthNumber, YearNumber
) GPSMonthlyAccruals
ON Months.MonthNumber = GPSMonthlyAccruals.MonthNumber
LEFT JOIN (
SELECT MonthNumber,
YearNumber,
SUM(Fee) AS Amount
FROM TradeCMEGPSMonthly
WHERE YearNumber = @year
AND RecordType = 'E'
AND Expired_CarryoverIndicator NOT IN ('E','C')
GROUP BY MonthNumber, YearNumber
) GPSMonthlyAccrualsReceivable
ON Months.MonthNumber = GPSMonthlyAccrualsReceivable.MonthNumber
LEFT JOIN (
SELECT MONTH(TradeDate) AS MonthNumber,
YEAR(TradeDate) AS YearNumber,
SUM(CASE WHEN AssetClassCode = 'Z' THEN PCOMM * -1 ELSE POTHER END)*-1 AS Amount
FROM vFutureCMEGPSGMIPay
WHERE YEAR(TradeDate) = @year
AND StatusID <> 4
GROUP BY MONTH(TradeDate), YEAR(TradeDate)
) GMIDailyAccruals
ON Months.MonthNumber = GMIDailyAccruals.MonthNumber
LEFT JOIN (
SELECT
MonthNumber,
YearNumber,
SUM(AMOUNT)*-1 AS Amount
FROM (
SELECT DISTINCT *
FROM GMIGeneralLedger
WHERE YearNumber = @year
AND GL_ACCOUNT_NBR = '4402795'
AND ATTRIBUTE1 NOT LIKE '%GPS SETTLEMENT%'
) GMIGeneralLedger
GROUP BY MonthNumber, YearNumber
) GLPayable
ON Months.MonthNumber = GLPayable.MonthNumber
LEFT JOIN (
SELECT
MonthNumber,
YearNumber,
SUM(AMOUNT) AS Amount
FROM (
SELECT DISTINCT *
FROM GMIGeneralLedger
WHERE YearNumber = @year
AND GL_ACCOUNT_NBR = '4402795'
AND ATTRIBUTE1 LIKE '%GPS SETTLEMENT%'
) GMIGeneralLedger
GROUP BY MonthNumber, YearNumber
) Paid
ON Months.MonthNumber = Paid.MonthNumber
LEFT JOIN (
SELECT AdjustmentMonth,
AdjustmentYear,
SUM(Amount)*-1 AS Amount
FROM AdjustmentFutureWFSPayable
WHERE ModuleID = 9
AND AdjustmentYear = @year
GROUP BY AdjustmentMonth, AdjustmentYear
) AdjustmentsLateTradeAmount
ON Months.MonthNumber = AdjustmentsLateTradeAmount.AdjustmentMonth
LEFT JOIN (
SELECT MONTH(TradeDate) AS MonthNumber,
YEAR(TradeDate) AS YearNumber,
COUNT(TradeStatusID) AS Amount
FROM TradeCMEGPS
INNER JOIN TradeStatus ON TradeCMEGPS.TradeID = TradeStatus.TradeID AND ModuleID = 9 AND SourceSystemID = 7
WHERE YEAR(TradeDate) = @year
AND TradeStatus.StatusID <> 4
GROUP BY MONTH(TradeDate), YEAR(TradeDate)
) TotalTrades
ON Months.MonthNumber = TotalTrades.MonthNumber
LEFT JOIN (
SELECT MONTH(TradeDate) AS MonthNumber,
YEAR(TradeDate) AS YearNumber,
COUNT(TradeStatusID) AS Amount
FROM TradeCMEGPS
INNER JOIN TradeStatus ON TradeCMEGPS.TradeID = TradeStatus.TradeID AND ModuleID = 9 AND SourceSystemID = 7
WHERE YEAR(TradeDate) = @year
AND MatchingGroupID IS NOT NULL
GROUP BY MONTH(TradeDate), YEAR(TradeDate)
) MatchedTrades
ON Months.MonthNumber = MatchedTrades.MonthNumber