Tsql比较结果集中的两个字段

时间:2021-08-25 16:29:20

I have two tables : table_A and table_W. I want to get only the non-matching payment details with the corresponding descriptions. The table are:

我有两个表:table_A和table_W。我想只获得不匹配的付款细节和相应的说明。表格是:

table_W

adharno phone descrip_w amount_w
1       11    p1        100
2       22    p1        250
2       22    p2        250
2       22    p3        300
2       33    p1        150
2       33    p2        150
2       33    p3        400
3       55    p1        50
3       66    p1        100

table_A

adharno phone decrip_a  amount_a
1       11    p1        110
2       22    p1        150
2       22    p2        150
2       22    p3        400
2       33    p1        250
2       33    p2        250
2       33    p3        300
3       55    p1        100
3       66    p1        50

I am getting the following result.

我得到以下结果。

adharno phone descrip_w amount_w decrip_a amount_a
1       11    p1        100      p1       110
2       22    p1        250      p1       150
2       22    p2        250      p2       150
2       22    p3        300      p3       400
2       33    p1        150      p1       250
2       33    p2        150      p2       250
2       33    p3        400      p3       300
3       55    p1        50       p1       100
3       66    p1        100      p1       50

But the result I am looking is as follows

但我看的结果如下

adharno phone descrip_w amount_w decrip_a amount_a
1       11    p1        100      p1       110

I want to compare the matching descrip, even if the phoneno is different but the adhaarno should be same.

我想比较匹配的描述,即使phoneno不同但adhaarno应该是相同的。

1 个解决方案

#1


1  

How about:

SELECT w.adharno,
       w.phone w_phone,
       descrip_w,
       amount_w,
       a.phone a_phone,
       decrip_a,
       amount_a
FROM table_w w
JOIN table_a a ON a.adharno = w.adharno
              AND w.descrip_w = a.decrip_a
              AND (w.phone <> a.phone
                OR w.amount_w <> a.amount_a)

#1


1  

How about:

SELECT w.adharno,
       w.phone w_phone,
       descrip_w,
       amount_w,
       a.phone a_phone,
       decrip_a,
       amount_a
FROM table_w w
JOIN table_a a ON a.adharno = w.adharno
              AND w.descrip_w = a.decrip_a
              AND (w.phone <> a.phone
                OR w.amount_w <> a.amount_a)