2013-05-28 42 views
0

我的問題是要獲取ABC的最大和最小日期。我需要做大約20萬條記錄,這需要更多的時間。如何提高性能要計算海量記錄的最大和最小記錄

ROW_ID  DATE     C  value 
---------------------------------------------- 
1 2012-08-01 00:00:00.0 ABC  87 
2 2012-09-01 00:00:00.0 ABC  87 
3 2012-10-01 00:00:00.0 ABC  87 
4 2012-11-01 00:00:00.0 ABC  87 
5 2012-12-01 00:00:00.0 ABC  87 
6 2013-01-01 00:00:00.0 CBA  87 
7 2013-02-01 00:00:00.0 ABC  87 
8 2013-03-01 00:00:00.0 ABC  87 

回答

2

你應該能夠做到這一點很容易使用的東西,如:

select c, 
    min(date) min_date, 
    max(date) max_date 
from yt 
where c='ABC' 
group by c; 

SQL Fiddle with Demo

編輯,因爲您試圖使用此數據更新Sybase中的其他表,因此您有幾個選項。 Sybase不容許在UPDATE語句中派生表,所以我會建議使用臨時表來獲得關於C的最小/最大日期,然後在您的更新使用此表的連接:

select c, 
    min(date) min_date, 
    max(date) max_date 
into #temp2 
from yt 
where c='ABC' 
group by c; 

update t 
set t.min_date = t1.min_date, 
    t.max_date = t1.max_date 
from temp t 
inner join #temp2 t1 
    on t.c = t1.c; 

SQL Fiddle with Demo

+0

感謝bluefeet。但問題是我必須在更新查詢中使用它。更新查詢不支持group by子句。 – antosnowin

+0

@antosnowin你在更新什麼?請編輯您的OP與您的要求的確切細節 – Taryn

+0

我將基於'C'值更新最小值和最大值到另一個表..我需要在更新查詢相同的邏輯。 – antosnowin