Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MSSQL-获取另一个表中字段中第一个较大值的值_Sql_Sql Server_Join - Fatal编程技术网

MSSQL-获取另一个表中字段中第一个较大值的值

MSSQL-获取另一个表中字段中第一个较大值的值,sql,sql-server,join,Sql,Sql Server,Join,查询从表A返回以下结果集: Condition Ranking Cell 103 Cell 142 CM 124 ECM 423 CM 105 ECM 404 在一个单独的表B中,对于上述三种情况,我有一个加和的值,如下所示: Condition Bins IncreasingSum Cell Bin1 0 Cell Bin2 1 Cell

查询从
表A
返回以下结果集:

Condition   Ranking
Cell        103
Cell        142
CM          124
ECM         423
CM          105
ECM         404
在一个单独的
表B
中,对于上述三种情况,我有一个加和的值,如下所示:

Condition   Bins    IncreasingSum
Cell        Bin1    0
Cell        Bin2    1
Cell        Bin3    1
Cell        Bin4    10
Cell        Bin5    36
Cell        Bin6    94
Cell        Bin7    223
....        ....    ...
ECM         Bin1    2
ECM         Bin2    4
ECM         Bin3    11
ECM         Bin4    34
ECM         Bin5    77
ECM         Bin6    166
ECM         Bin7    326
ECM         Bin8    540
ECM         Bin9    843
....        ....    ....
CM          Bin1    2
CM          Bin2    12
CM          Bin3    35
CM          Bin4    114
CM          Bin5    233
CM          Bin6    429
Condition   Ranking    Bin
Cell        103        Bin7
Cell        142        Bin7
CM          124        Bin5
ECM         423        Bin8
CM          105        Bin4
ECM         404        Bin8
我想加入这些表,并获得第一个值大于条件所属排名的Bin的名称。更具体地说,结果如下所示:

Condition   Bins    IncreasingSum
Cell        Bin1    0
Cell        Bin2    1
Cell        Bin3    1
Cell        Bin4    10
Cell        Bin5    36
Cell        Bin6    94
Cell        Bin7    223
....        ....    ...
ECM         Bin1    2
ECM         Bin2    4
ECM         Bin3    11
ECM         Bin4    34
ECM         Bin5    77
ECM         Bin6    166
ECM         Bin7    326
ECM         Bin8    540
ECM         Bin9    843
....        ....    ....
CM          Bin1    2
CM          Bin2    12
CM          Bin3    35
CM          Bin4    114
CM          Bin5    233
CM          Bin6    429
Condition   Ranking    Bin
Cell        103        Bin7
Cell        142        Bin7
CM          124        Bin5
ECM         423        Bin8
CM          105        Bin4
ECM         404        Bin8

有什么建议吗?

您可以使用
应用

select a.*, b.bin
from tableA a outer apply
     (select top (1) b.bin
      from tableB b
      where b.condition = a.condition and
            b.increasingsum > a.ranking
      order by b.increasingsum asc
     ) b;

您可以使用
apply

select a.*, b.bin
from tableA a outer apply
     (select top (1) b.bin
      from tableB b
      where b.condition = a.condition and
            b.increasingsum > a.ranking
      order by b.increasingsum asc
     ) b;

如果没有更大的价值呢?如果没有更大的价值呢?