2017-10-17 191 views
0

我有一個SQL Server 2008 R2數據庫,包含兩個表:hystrealalarmSQL Server:查找表中的前5個元素,並在另一個表中找到前5個元素

alarm中,我有一個警告的字典和在某些條件下增加的事件。由於有一些線程有時間註冊這些警報,因此在hystreal中我註冊了一個警報和線程註冊它的時間戳。

因此,hystreal看起來是這樣的:

dataregvalue          timestamp    
-------------------------------------------------------------------------- 
ST8 err. cons.          1506352039    
ST8 err. cons.          1506352049    
ST8 err. cons.          1506352060    
ST8 err. cons.          1506352070    
ST8 err. cons.          1506352081    
ST8 err. cons.          1506352091    
ST8 err. cons.          1506352102    
ST8 err. cons.          1506352112    
ST8 err. cons.          1506352123    
ST8 err. cons.          1506352133    
ST8 err. cons.          1506352144    
ST8 err. cons.          1506352154    
ST8 err. cons.          1506352165    
ST7 timeout           1506352448    
ST7 timeout           1506352458    
ST7 timeout           1506352469    
ST7 timeout           1506352479    
ST7 timeout           1506352490    
ST7 timeout           1506352500    
ST7 timeout           1506352511    
ST7 timeout           1506352532    
ST7 timeout           1506352543    
ST7 timeout           1506352553    
ST7 timeout           1506352564    
ST7 timeout           1506352585    
ST7 timeout           1506352595    
ST7 timeout           1506353273    
ST7 timeout           1506353283    
ST7 timeout           1506353293    
mac. stop           1506353367    
mac. stop           1506353399    
mac. stop           1506353420    
mac. stop           1506353441    
ST3 timeout           1506353714    
ST3 timeout           1506353724    
ST3 timeout           1506353735    
ST3 timeout           1506353788    
ST13 timeout          1506353809    
ST13 timeout          1506353819    
ST23 err. Z42          1506353893    
ST23 err. Z42          1506353904    
ST23 err. Z42          1506353914    
ST23 err. Z42          1506353925    
ST23 err. Z42          1506353935    
ST23 err. Z42          1506353945    
ST23 err. Z42          1506353956    
ST23 err. Z42          1506353966    
ST23 err. Z42          1506353977    
ST23 err. Z42          1506353988    
ST23 err. Z42          1506353998    
ST23 err. Z42          1506354009    
ST23 err. Z42          1506354019    
ST23 err. Z42          1506354030    
ST23 err. Z42          1506354041    
ST7 timeout           1506354157    
ST7 timeout           1506354167    
ST7 timeout           1506354178    
ST7 timeout           1506354188    
ST7 timeout           1506354757    
ST7 timeout           1506354767    
ST7 timeout           1506354778    
ST7 timeout           1506354789   

一會兒,alarm看起來是這樣的:

communication          occur 
------------------------------------------------------------ 
ST8 err. cons.          75    
ST7 timeout           15    
mac. stop           43    
ST3 timeout           7    
ST13 timeout          33    
ST23 err. Z42          1    

我想找到前5 communication(前5基於價值occur)的alarms,其所有相對時間戳爲hystreal。如何做到這一點?提前致謝!

N.B .: timestamp的數量不是與價值occur相關。可以有更多的timestamp

+1

我不理解你想要的輸出。前5名基於什麼標準?根據您的樣本數據,您期望輸出什麼? –

+0

我寫到前5名應該是基於發生。 –

+0

我知道你是基於發生的價值寫下來的,但這是什麼意思?最大的價值,最低的價值?仍然不明白你對輸出的期望。 –

回答

2

它可以是如此簡單:

Select * from hystreal 
where dataregvalue in 
(
Select top 5 communication 
from alarm 
order by occur 
) 
相關問題