sql - SQL(MS Access):对于给定的记录,如何从给定的一组字段中查询前 5 个值?
问题描述
我有一个包含几十个字段的表,其中 18 个是具有不同整数值的字段。我想为给定记录提取具有最大值的五个字段的字段名称和相应值。我将使用下表作为示例,我只想查询给定记录的两个最大值。
Name, FigureA, FigureB, FigureC
John, 40, 73, 81
Luke, 35, 21, 65
我想为约翰返回以下内容:
FigureB, 73
FigureC, 81
我已经做到了这一点:
sSQL = "Select t.* " & _
"From (Select 'A' as [FigureA], FigureA " & _
"From Table) as t " & _
"Union All " & _
"Select t.* " & _
"From (Select 'B' as [FigureB], FigureB " & _
"From Table) as t " & _
"Union All " & _
"Select t.* " & _
"From (Select 'C' as [FigureC], FigureC " & _
"From Table) as t"
解决方案
在 MS Access 中,这可能是最简单的union all
:
select top (5) *
from (select t.*
from (select top (1) "field1" as colname, max(field1) as max_val
from t
) as t
union all
select t.*
from (select top (1) "field2" as colname, max(field2)
from t
) as t
union all
. . .
) as t
order by max_val desc;
某些版本的 MS Access 不支持union all
该from
子句。该组件可能需要视图。
推荐阅读
- spring - Mockito spy 抛出存根异常
- python-2.7 - 在 Alexa 开发者控制台中,如何获取要在主动 api 中使用的客户端 ID 和客户端秘密访问密钥
- angular - 想在 Angular 7 应用程序中使用 Web 组件(在 reactJS 中创建)
- c# - 确定在 WPF 容器中触发上下文菜单的绑定对象?
- javascript - 如何使用 jquery 更改动画速度?
- angular - @ViewChild 返回未定义 - Angular 2
- android - 哪些版本的 Android NDK 支持 64 位架构?
- git - git 将部分更改从“待提交”移动到本地更改
- r - 在 data.frame 中传播和排序连接的因子变量
- jquery - 如何根据所选选项包含特定表格