2016-07-25 73 views
0

自動加入存儲過程中的表需要很長時間。如何從自加入表中獲取最大停止日期

請建議其他有效的方法來做到這一點。

查詢

Select a.*,b.* from acct a join acct b 
On a.acctno=b.acctno 
Join finance c 
On a.acctno=c.acctno 
Where a.insertdate between start_date and end_date 
And b.stop_date=(select max(stop_date) from acct where acctno=b.acctno and addr_code=b.addr_code and unique_id<>a.unique_id and stop_date<=end_date) 
+0

你在DB上設置了索引嗎? – Grommy

+0

stop_date上沒有索引,我不應該在列上添加索引。我正在使用SQL管理工作室2012 –

+0

爲什麼你需要聯合acct本身?這是一對一還是多對多的連接?如果它是一對一的,那麼你只是看着相同的記錄,如果它是很多很多,那麼你正在創建一個交叉連接矩陣的記錄 – Cato

回答

0

利用這一點,希望這將有助於。

Select a.*,b.* from acct a,acct b,finance c 
where a.acctno=b.acctno 
and a.acctno=c.acctno 
and a.insertdate 
between a.start_date and a.end_date 
And b.stop_date=(select max(stop_date) from acct a,acct b 
where a.acctno=b.acctno 
and a.addr_code=b.addr_code 
and a.unique_id<>b.unique_id 
and a.stop_date<=a.end_date)