针对子查询的查询产生与单独 运行 子查询不同的结果

Query against subquery yields different results than running subquery alone

我有一个使用子查询对数据进行子集化的查询,然后我正尝试从子查询中 select 特定数据。子查询是:

select top 10 Build_ID, Appscan_Definitive_High,
rank() over (order by Appscan_Definitive_High desc) as rankpct
from 
(
select build_id, convert(int,appscan_definitive_high) as
           appscan_definitive_high
from dbo.SDFBuildMetrics
where coalesce(appscan_definitive_high,0)>0
) a 

结果如下:

Build_ID    Appscan_Definitive_High rankpct
31966   51  1
32627   51  1
44293   51  1
47011   51  1
47968   51  1
48554   51  1
25586   49  7
27370   49  7
40357   48  9
23867   44  10

但是当我运行一个针对子查询的查询时:

select Appscan_Definitive_High
from
(
select top 10 Build_ID, Appscan_Definitive_High,
    rank() over (order by Appscan_Definitive_High desc) as rankpct
from 
(
    select build_id, convert(int,appscan_definitive_high) as
                appscan_definitive_high
    from dbo.SDFBuildMetrics
    where coalesce(appscan_definitive_high,0)>0
) a 
 ) aa

我得到:

Appscan_Definitive_High
1
44
21
44
2
44
2
6
7
7

完整查询的最终目的是检索 min(AppScan_Definitive_High) 但由于返回的值集与子查询返回的值集不匹配,min 函数不给出我需要什么。我假设子查询 returns 是外部查询所针对的一组数据,但在上面的示例中似乎并非如此。

有什么帮助吗?

你是 运行 select 前 10 名,没有排序。要获得您想要的结果,您需要在那里订购。