我必須執行此查詢這需要大約52秒我已經意味着使用遊標或者無論如何,可以幫助我優化執行時間SQL查詢優化
select * from (
select distinct 'Net Sales' Class, SUM (CAST(ROUND(ABS(dbo.Trial_Balance_Variances.PTD_ACTUAL), 0) AS float)) over (partition by CONVERT(int,CONVERT(varchar(10), dbo.Trial_Balance_Variances.PERIOD_DATE, 112)))
- (select distinct sum(CAST(ROUND(ABS(v.PTD_ACTUAL), 0) AS float)) over (partition by CONVERT(int,CONVERT(varchar(10), v.PERIOD_DATE, 112))) from Trial_Balance_Variances v
inner join
dbo.[P&L] p ON v.GL_ACCOUNT_NUMBER = p.Code where p.Category in('Sales Rebates')
and v.PERIOD_NAME = Trial_Balance_Variances.PERIOD_NAME
) Total,CONVERT(int,CONVERT(varchar(10), dbo.Trial_Balance_Variances.PERIOD_DATE, 112)) AS DateKey
from Trial_Balance_Variances
inner join
dbo.[P&L] ON dbo.Trial_Balance_Variances.GL_ACCOUNT_NUMBER = dbo.[P&L].Code LEFT OUTER JOIN
dbo.Location AS l ON l.Code = SUBSTRING(CONVERT(varchar(50), dbo.Trial_Balance_Variances.ACCOUNT_COMBINATION), 4, 2) where Category = 'Potash Sales (Gross)'
union
SELECT distinct l.Class, SUM (CAST(ROUND(ABS(dbo.Trial_Balance_Variances.PTD_ACTUAL), 0) AS float)) over (partition by CONVERT(int,CONVERT(varchar(10), dbo.Trial_Balance_Variances.PERIOD_DATE, 112)))
+ (select distinct sum(CAST(ROUND(ABS(v.PTD_ACTUAL), 0) AS float)) over (partition by CONVERT(int,CONVERT(varchar(10), v.PERIOD_DATE, 112))) from Trial_Balance_Variances v
inner join
dbo.[P&L] p ON v.GL_ACCOUNT_NUMBER = p.Code where p.Category in('Change In Inventory','Freight','Dispatch')
and v.PERIOD_NAME = Trial_Balance_Variances.PERIOD_NAME
) Total
, CONVERT(int,CONVERT(varchar(10), dbo.Trial_Balance_Variances.PERIOD_DATE, 112)) AS DateKey from Trial_Balance_Variances
inner join
dbo.[P&L] ON dbo.Trial_Balance_Variances.GL_ACCOUNT_NUMBER = dbo.[P&L].Code LEFT OUTER JOIN
dbo.Location AS l ON l.Code = SUBSTRING(CONVERT(varchar(50), dbo.Trial_Balance_Variances.ACCOUNT_COMBINATION), 4, 2)
where Class='Cost of goods sold' and Category not in ('Sales Rebates','Royalties','Potash Sales (Gross)','Interest Expense LTL','Interest Expense LTMed','Interest Expense Others'
,'Interest Income','(Gain)/Loss from Investment Jor','(Gain)/Loss from Investment N+B','Gain from Investment', 'EXC Gain from Investment' ,'D Gain from Investment ', 'Loss from Investment Jormag','Bank Charges','Other Revenues','Change In Inventory','Freight','Dispatch')
union
SELECT distinct l.Class, SUM (CAST(ROUND(ABS(dbo.Trial_Balance_Variances.PTD_ACTUAL), 0) AS float)) over (partition by CONVERT(int,CONVERT(varchar(10), dbo.Trial_Balance_Variances.PERIOD_DATE, 112))) Total
, CONVERT(int,CONVERT(varchar(10), dbo.Trial_Balance_Variances.PERIOD_DATE, 112)) AS DateKey from Trial_Balance_Variances
inner join
dbo.[P&L] ON dbo.Trial_Balance_Variances.GL_ACCOUNT_NUMBER = dbo.[P&L].Code LEFT OUTER JOIN
dbo.Location AS l ON l.Code = SUBSTRING(CONVERT(varchar(50), dbo.Trial_Balance_Variances.ACCOUNT_COMBINATION), 4, 2)
where Class='Administration' and Category not in ('Sales Rebates','Royalties','Potash Sales (Gross)','Interest Expense LTL','Interest Expense LTMed','Interest Expense Others'
,'Interest Income','(Gain)/Loss from Investment Jor','(Gain)/Loss from Investment N+B','Gain from Investment', 'EXC Gain from Investment' ,'D Gain from Investment ', 'Loss from Investment Jormag','Bank Charges','Change In Inventory','Freight','Dispatch','Other Revenues')
union
SELECT distinct l.Class, SUM (CAST(ROUND(ABS(dbo.Trial_Balance_Variances.PTD_ACTUAL), 0) AS float)) over (partition by CONVERT(int,CONVERT(varchar(10), dbo.Trial_Balance_Variances.PERIOD_DATE, 112))) Total
, CONVERT(int,CONVERT(varchar(10), dbo.Trial_Balance_Variances.PERIOD_DATE, 112)) AS DateKey from Trial_Balance_Variances
inner join
dbo.[P&L] ON dbo.Trial_Balance_Variances.GL_ACCOUNT_NUMBER = dbo.[P&L].Code LEFT OUTER JOIN
dbo.Location AS l ON l.Code = SUBSTRING(CONVERT(varchar(50), dbo.Trial_Balance_Variances.ACCOUNT_COMBINATION), 4, 2)
where Class='Selling And Distribution Expenses' and Category not in ('Sales Rebates','Royalties','Potash Sales (Gross)','Interest Expense LTL','Interest Expense LTMed','Interest Expense Others'
,'Interest Income','(Gain)/Loss from Investment Jor','(Gain)/Loss from Investment N+B','Gain from Investment', 'EXC Gain from Investment' ,'D Gain from Investment ', 'Loss from Investment Jormag','Bank Charges','Change In Inventory','Freight','Dispatch','Other Revenues')
union
SELECT distinct l.Class, SUM (CAST(ROUND(ABS(dbo.Trial_Balance_Variances.PTD_ACTUAL), 0) AS float)) over (partition by CONVERT(int,CONVERT(varchar(10), dbo.Trial_Balance_Variances.PERIOD_DATE, 112)))
+ (select distinct sum(CAST(ROUND(ABS(v.PTD_ACTUAL), 0) AS float)) over (partition by CONVERT(int,CONVERT(varchar(10), v.PERIOD_DATE, 112))) from Trial_Balance_Variances v
inner join
dbo.[P&L] p ON v.GL_ACCOUNT_NUMBER = p.Code where p.Category in('Interest Expense LTL','Interest Expense LTMed','Interest Expense Others'
,'Interest Income','(Gain)/Loss from Investment Jor','(Gain)/Loss from Investment N+B','Gain from Investment', 'EXC Gain from Investment' ,'D Gain from Investment ', 'Loss from Investment Jormag','Bank Charges','Other Revenues')
and v.PERIOD_NAME = Trial_Balance_Variances.PERIOD_NAME and SUBSTRING(CONVERT(varchar(50), v.ACCOUNT_COMBINATION), 4, 2) not in ('00','05')
)
, CONVERT(int,CONVERT(varchar(10), dbo.Trial_Balance_Variances.PERIOD_DATE, 112)) AS DateKey from Trial_Balance_Variances
inner join
dbo.[P&L] ON dbo.Trial_Balance_Variances.GL_ACCOUNT_NUMBER = dbo.[P&L].Code LEFT OUTER JOIN
dbo.Location AS l ON l.Code = SUBSTRING(CONVERT(varchar(50), dbo.Trial_Balance_Variances.ACCOUNT_COMBINATION), 4, 2)
where Class='Others' and
Category not in ('Sales Rebates','Royalties','Potash Sales (Gross)','Change In Inventory','Freight','Dispatch')) x order by x.DateKey
誰能幫我解決這個問題請問?
向此添加遊標絕對不會提高性能 - 完全相反!避免遊標,如果你可以..... –
Distincts ...到處 – JamieD77
那麼我能做什麼 –