从SQL Server中两个表之间的连接的结果集中查找第一个匹配项

时间:2017-05-18 09:07:00

标签: sql sql-server join

我有一张像这样的表#testrades

CREATE TABLE #testtrades
(
    TradeID int,
    producttype varchar(10),
    tradeddate date,
    settledate date,
    busunit varchar(5),
    qty int,
    price float,
    amount float,
    tradeside varchar(1),
    buysell varchar(1)
)

INSERT #testtrades
SELECT 1,'equity',getdate(),getdate()+3,'bus1',10,100,1000,'S','B'
INSERT #testtrades
SELECT 2,'equity',getdate(),getdate()+3,'bus1',10,100,950,'C','S'
INSERT #testtrades
SELECT 3,'equity',getdate(),getdate()+3,'bus2',11,100,1000,'S','B'
INSERT #testtrades
SELECT 4,'equity',getdate(),getdate()+3,'bus3',10,100,1200,'S','S'
INSERT #testtrades
SELECT 5,'equity',getdate(),getdate()+3,'bus1',10,100,1200,'C','B'
INSERT #testtrades
SELECT 6,'equity',getdate(),getdate()+3,'bus2',10,100,1000,'C','S'
INSERT #testtrades
SELECT 7,'equity',getdate(),getdate()+3,'bus4',10,100,1000,'C','B'
INSERT #testtrades
SELECT 8,'equity',getdate(),getdate()+3,'bus5',10,100,950,'S','S'
INSERT #testtrades
SELECT 9,'equity',getdate(),getdate()+3,'bus5',10,100,1200,'C','S'
INSERT #testtrades
SELECT 10,'equity',getdate(),getdate()+3,'bus4',14,100,1000,'S','B'
INSERT #testtrades
SELECT 11,'equity',getdate(),getdate()+3,'bus6',10,100,1000,'C','S'
INSERT #testtrades
SELECT 12,'equity',getdate(),getdate()+3,'bus7',10,100,950,'C','B'
INSERT #testtrades
SELECT 13,'equity',getdate(),getdate()+3,'bus7',10,100,1200,'C','S'
INSERT #testtrades
SELECT 14,'equity',getdate(),getdate()+3,'bus7',10,100,1000,'S','S'

INSERT #sideA
SELECT * FROM #testtrades
WHERE tradeside='C'

INSERT #sideB
SELECT * FROM #testtrades
WHERE  tradeside='S'

SELECT 
    ROW_NUMBER() OVER (ORDER BY A.tradeID), 
    A.TradeID, B.TradeID 
FROM 
    #sideA A 
JOIN 
    #sideB B ON A.buysell = B.buysell
             AND A.price = B.price
             AND A.Qty = B.Qty
             AND ABS(A.Amount - B.Amount) <= 50

结果集:

id tradeidA tradeidB
--------------------
 1   2         8
 2   2        14
 3   6         8
 4   6        14
 5   7         1
 6   9         4
 7  11         8
 8  11        14
 9  12         1
10  13         4

从上面的结果集中,我只想检索第一个匹配的行,例如tradeIDA 2与8和14匹配。我只想检索2,8。然后6首先匹配8。但由于8已经与2匹配,因此6不符合条件。然后6与14匹配,所以我想要检索该记录。预期的结果集如下所示。

id tradeIDA tradeIDB
--------------------
1     2       8
4     6      14
5     7       1
6     9       4

1 个答案:

答案 0 :(得分:0)

也许这可以给你一个想法。

    ;
        WITH    CTE
                  AS ( SELECT   ROW_NUMBER() OVER ( ORDER BY A.tradeID ) rownum ,
                                A.TradeID TradeA ,
                                B.TradeID TradeB
                       FROM     #sideA A
                                JOIN #sideB B ON A.buysell = B.buysell
                                                 AND A.price = B.price
                                                 AND A.Qty = B.Qty
                                                 AND ABS(A.Amount - B.Amount) <= 50
                     ),
                CTE1
                  AS ( SELECT   rownum ,
                                TradeA ,
                                ROW_NUMBER() OVER ( ORDER BY rownum ) ctr
                       FROM     ( SELECT    rownum ,
                                            TradeA ,
                                            ROW_NUMBER() OVER ( PARTITION BY TradeA ORDER BY rownum ) ctr
                                  FROM      cte
                                ) T
                       WHERE    t.ctr = 1
                     ),
                CTE2
                  AS ( SELECT   TradeB ,
                                ROW_NUMBER() OVER ( ORDER BY rownum ) ctr
                       FROM     ( SELECT    rownum ,
                                            TradeB ,
                                            ROW_NUMBER() OVER ( PARTITION BY TradeB ORDER BY rownum ) ctr
                                  FROM      cte
                                ) T
                       WHERE    t.ctr = 1
                     ),
                CTE3
                  AS ( SELECT   CTE1.TradeA  [tradeIDA],
                                CTE2.TradeB  [tradeIDB]
                       FROM     CTE1
                                JOIN CTE2 ON CTE2.ctr = CTE1.ctr
                     )
            SELECT  *
            FROM    CTE3

结果:

tradeIDA    tradeIDB
----------- -----------
2           8
6           14
7           1
9           4

(4 row(s) affected)