简体   繁体   English

SQL Server在主表中连接两个没有重复的表

[英]SQL Server Join two tables without duplicates in primary table

I have two tables that I want to join together such that all foreign rows are returned and the primary table's rows are not duplicated. 我有两个表,我想连接在一起,以便返回所有外部行,并且主表的行不重复。 For example: 例如:

T1  

    pk  code  value
    1   One    100
    2   Two    200

T2  

    fk   value  
    1     10  
    1     15  
    1     30  
    2     25  

I want all records of T2 without the T1 records duplicating, so the result set I want to look like this: 我希望T2的所有记录都没有重复的T1记录,所以结果集我想看起来像这样:

 T2.fk   T1.code  T1.value T2.value  
    1      One      100       10  
    1      NULL     NULL      15  
    1      NULL     NULL      30  
    2      Two      200       25  

Is there a SQL Server join method for achieving that? 是否有SQL Server连接方法来实现这一目标?

You need to rank your rows in T2 and do a left join including rank as a join condition: 您需要在T2对行进行排名并执行left join包括排名作为连接条件:

with cte as(select *, row_number() over(partition by fk order by value) as rn from T2)

select c.fk, t.code, t.value, c.value 
from cte c
left join T1 t on c.fk = t.pk and c.rn = 1

Here is the full example: 这是完整的例子:

DECLARE @t1 TABLE
    (
      pk INT ,
      code VARCHAR(MAX) ,
      value INT
    )
INSERT  INTO @t1
VALUES  ( 1, 'One', 100 ),
        ( 2, 'Two', 200 )

DECLARE @t2 TABLE ( fk INT, value INT )
INSERT  INTO @t2
VALUES  ( 1, 10 ),
        ( 1, 15 ),
        ( 1, 30 ),
        ( 2, 25 );
WITH    cte
          AS ( SELECT   * ,
                        ROW_NUMBER() OVER ( PARTITION BY fk ORDER BY value ) AS rn
               FROM     @t2
             )
    SELECT  c.fk ,
            t.code ,
            t.value ,
            c.value
    FROM    cte c
            LEFT JOIN @t1 t ON c.fk = t.pk
                               AND c.rn = 1

Try this: 尝试这个:

select T2.fk, 
CASE
    WHEN (SELECT COUNT(*) FROM t2 tother WHERE tother.fk = t2.fk
    AND tother.value > t2.value) > 0 THEN NULL ELSE t1.code
END,
CASE
    WHEN (SELECT COUNT(*) FROM t2 tother WHERE tother.fk = t2.fk
    AND tother.value > t2.value) > 0 THEN NULL ELSE t1.value
END,T2.value  
from t2
join t1
on t2.fk = t1.pk
    DECLARE @t1 TABLE (pk int,code varchar(10),value int)
    DECLARE @t2 TABLE (fk int,value int)

    INSERT INTO @t1
    SELECT 1,'one',100
    UNION
    SELECT 2,'two',200

    INSERT INTO @t2
    SELECT 1,10
    UNION SELECT 1,15 UNION SELECT 1,30 UNION SELECT 2,25


;WITH cte AS(
SELECT t2.fk,t2.value t2val,t1.pk,t1.code,t1.value t1val,ROW_NUMBER() OVER(PARTITION BY fk ORDER BY fk) rno FROM @t2 t2 LEFT JOIN @t1 t1 on t2.fk=t1.pk)
SELECT fk,code=(CASE WHEN rno=1 THEN code ELSE null END),t1val=(CASE WHEN rno=1 THEN t1val ELSE NULL END),t2val FROM cte

output 产量

fk  code    t1val   t2val
1   one     100      10
1   NULL    NULL     15
1   NULL    NULL     30
2   two     200      25

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

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