SQL Server:查找表中的前5个元素,并将其全部出现在另一个表中

时间:2017-10-17 14:26:39

标签: sql-server sql-server-2008-r2

我有一个带有两个表的SQL Server 2008 R2数据库:hystrealalarm

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            

我想在communication中找到前5个occur(基于alarms的值排名前5位),其相对时间戳位于hystreal。怎么办?提前谢谢!

N.B。:与timestamp的值相关的occur 的数量。还有更多timestamp

1 个答案:

答案 0 :(得分:2)

可以这么简单:

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