查询每个窗口小部件最近的两个条目 [英] Query most recent TWO entries per widget

查看:126
本文介绍了查询每个窗口小部件最近的两个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子。一个(小部件)有一个小部件列表(ID,小部件名称,颜色等)和有关它们的数据。
另一个(测试)有一个在窗口小部件上运行的测试列表(ID,date,info1,info2等)。



我想做的是显示最近的TWO测试。我不认为我真的需要使用表Widget为此,但我描述了,所以你会知道哪里从哪里来。



我已经构建了一个使用MAX()函数的总计查询,并显示每个ID的单个最近日期。然后,我可以使用此查询来构建另一个查询,该查询显示该小部件的该日期发生的测试的有用信息。我真正需要的是,每个小部件最近都有两个测试日期。

解决方案

如果你需要两个最近的测试,然后

 选择*从测试T 
其中(选择计数(*)从测试
其中testDate> T.TestDate) 2

如果您需要每个Widget最近的两个测试,那么

 选择*从测试T 
其中(选择计数(*)从测试
其中WidgetId = T.WidgetId
和testDate> T.TestDate) 2


I have two tables. One (Widgets) has a list of widgets (ID, widget_name, color, etc...) and data about them. The other one (Tests) has a list of tests run on the widgets (ID, date, info1, info2, etc...).

What I want to do is display the most recent TWO tests. I dont think i really need to use the table Widgets for this but i described it so you would know where im coming from.

I have constructed a "Totals" query that uses the MAX() function and displays the single most recent Date for each ID. I then can use this query to construct another query that displays useful information about the test that happened on that date for that widget. What i really need, though is to have the most recent two test dates for each widget.

解决方案

If you need the two most recent tests overall, then

Select * From Tests T
Where (Select Count(*) From tests
       Where testDate > T.TestDate) < 2

If you need the two most recent tests for each Widget, then

Select * From Tests T
Where (Select Count(*) From tests
       Where WidgetId = T.WidgetId 
           And testDate > T.TestDate) < 2

这篇关于查询每个窗口小部件最近的两个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆