简体   繁体   English

SQL查询:使用内部联接对结果行进行附加筛选

[英]SQL Query: Additional filtering of result rows using inner join

Given Table_1: 鉴于表1:

| testing | main_val |
| AB      | VAL      |
| EF      | DIP      |

and Table_2: 和表_2:

| main_val | for_testing |
| VAL      |             |
| VAL      | AB          |
| VAL      | CD          |
| DIP      |             |
| DIP      | GH          |

My join query is below: 我的加入查询如下:

select table1.testing, table1.main_val, table2.for_testing from table_1 table1
inner join table_2 table2 on table1.main_val = table2.main_val

I get the following result: 我得到以下结果:

| testing | main_val | for_testing |
| AB      | VAL      |             |
| AB      | VAL      | AB          |
| AB      | VAL      | CD          |
| EF      | DIP      |             |
| EF      | DIP      | GH          |

My requirement is to get the best "testing match". 我的要求是获得最佳的“测试匹配”。 If the testing and for_testing columns match, then I should retrieve that row. 如果testing和for_testing列匹配,那么我应该检索该行。 If no matches, choose the empty for_testing row. 如果没有匹配,请选择空的for_testing行。 I need the result to look like: 我需要结果看起来像:

| testing | main_val | for_testing |
| AB      | VAL      | AB          |
| EF      | DIP      |             |

More columns will be added for this join, that is why getting the best match row is important. 将为此连接添加更多列,这就是获得最佳匹配行的重要原因。

Try this: 尝试这个:

select table1.testing, table1.main_val, table2.for_testing 
from table_1 table1
inner join table_2 table2 on table1.main_val = table2.main_val 
where table1.testing =table2.for_testing 
union 
select table1.testing, table1.main_val, table2.for_testing 
from table_1 table1
inner join table_2 table2 on table1.main_val = table2.main_val 
where not in (select table1.testing, table1.main_val, table2.for_testing 
from table_1 table1
inner join table_2 table2 on table1.main_val = table2.main_val 
where table1.testing =table2.for_testing ) and t2.for_testing =""

Try this: 尝试这个:

;WITH CTE AS(
    SELECT 
        t1.*,
        t2.For_Testing,
        RN = ROW_NUMBER() 
                OVER(
                    PARTITION BY t1.Main_Val 
                    ORDER BY 
                        CASE 
                            WHEN t1.Testing = t2.For_Testing THEN 1
                            WHEN t2.For_Testing = '' THEN 2
                            ELSE 999
                        END
                    )
    FROM Table_1 t1
    INNER JOIN Table_2 t2
        ON t2.MAIN_val = t1.Main_Val
)
SELECT
    Testing,
    Main_val,
    For_Testing
FROM CTE
WHERE RN = 1
ORDER BY Testing

您必须使用Left Join并在加入时添加for_testing。

Try this... 尝试这个...

This is work for me 这对我有用

select Table_1.testing,Table_1.main_val,Table_2.for_testing from Table_2 right join

 Table_1 on Table_2.main_val=Table_1.main_val and Table_2.for_testing=Table_1.testing

Try this:- 尝试这个:-

SELECT table1.testing, table1.main_val, table2.for_testing 
FROM table_1 table1 INNER JOIN table_2 table2 
ON table1.main_val = table2.main_val
WHERE table1.testing = table2.for_testing
OR table1.testing IS NULL;

I think this can help you. 我想这可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM