SQL从2个表中拆分具有相同ID的值

时间:2019-02-05 01:17:29

标签: sql-server tsql ssms

我有2个表(正在从主表中检索数据)。示例:

表1

       id      GroupX     Source    GroupNum     Amount  
-------------------------------------------------------
 1     23      School     SH001     1            700  
 2     23      Bank       BA001     2            300  
 3     23      Music      MU001     3            500  
 4     23      School     SH999     1            900  

表2

     id    GroupNum     SourceAmt
----------------------------------
1    23    1            700  
2    23    2            100  
3    23    3            500  
4    23    1            900  

我的难题是我正在使用的查询。它会返回拆分值的其他行(表2“ GroupNum”中的拆分值分别为700和900)

我的结果应该是

     id    GroupX     Source    GroupNum     Amount     SourceAmt  
-----------------------------------------------------------------
 1   23    School     SH001     1            700        700  
 2   23    Bank       BA001     2            300        100  
 3   23    Music      MU001     3            500        500  
 4   23    School     SH999     1            900        900  

但是我得到了

     id    GroupX    Source     GroupNum     Amount     SourceAmt  
-----------------------------------------------------------------
 1   23    School     SH001     1            700        700  
 2   23    School     SH001     1            700        900  
 3   23    Bank       BA001     2            300        100  
 4   23    Music      MU001     3            500        500  
 5   23    School     SH999     1            900        900  
 6   23    School     SH999     1            900        700  

这是我的查询:

SELECT
    t1.id,
    t1.GroupX,
    t1.Source,
    t1.GroupNum,
    t1.Amount,
    t2.SourceAmt
FROM 
    table1 as t1
INNER JOIN 
    table2 as t2 ON t1.id = t2.id
                 AND t1.GroupNum = t2.GroupNum
WHERE 
    t1.id = 23 

我也尝试过使用Distinct。协助将不胜感激。

3 个答案:

答案 0 :(得分:1)

如果我的理解正确,您希望加入表1和表2,以使idGroupNum金额对齐。如果确实如此,那么您还需要加入相应的金额,例如:

Select t1.id, t1.Group, t1.Source, t1.GroupNum, t1.Amount, t2.SourceAmt
From table1 as t1 INNER JOIN
     table2 as t2 
     ON t1.id = t2.id AND t1.GroupNum = t2.GroupNum AND t1.Amount = t2.SourceAmt
where id = 23

如果这不是您想要的,或者您不想使用这些金额加入(例如,您不能保证不会多次看到相同的金额),那么您有点难题;您会注意到(idGroupNum)元组在任何一个表中都不都是唯一的,因此您的联接不是一对一的。您可能需要在Source中包含table2或在table1中提供一个transactionId,以映射到table2中的唯一ID列。

答案 1 :(得分:1)

您需要一个附加的join键。没有明显的候选人,也许只是数量上的候选人-但我不确定那是您的意图。 SQL表表示无序集,因此没有“行号”匹配的概念。

您可以使用row_number()分配行号。下面将进行匹配,但是您需要指定排序列:

Select t1.id, t1.Group, t1.Source, t1.GroupNum, t1.Amount, t2.SourceAmt
From (select t1.*,
             row_number() over (partition by t1.id order by ?) as seqnum
      from table1 t1
     ) t1 inner join
     (select t2.*
             row_number() over (partition by t1.id order by ?) as seqnum
      from table2 t2 
     ) t2
     on t1.id = t2.id and t1.GroupNum = t2.GroupNum and
        t1.seqnum = t2.seqnum
where id = 23 ;

?用于每个表中的排序列。

答案 2 :(得分:0)

我会选择与简单的116不同的方法,仅仅是因为您可以使用该结果集(这是该内部联接的结果集)来做有限的事情

我会进行多个联接。

首先,我将使用默认条件+ INNER JOIN LEFT JOIN。这将给我设置where table1.[Amount] = table2.[SourceAmt][Amount]相等的地方

此后,我使用默认条件[SourceAmt]来获取不匹配的金额

这是查询

INNER JOIN

这是结果集,您可以检查结果集

现在,我实际上将此结果用作预结果,并对with t1 as ( select 23 as [id], 'School' as [Group], 'SH001' as [Source], 1 as [GroupNum], 700 as [Amount] union all select 23, 'Bank', 'BA001', 2, 300 union all select 23, 'Music', 'MU001', 3, 500 union all select 23, 'School', 'SH999', 1, 900 ), t2 as ( select 23 as [id], 1 as [GroupNum], 700 as [SourceAmt] union all select 23, 2, 100 union all select 23, 3, 500 union all select 23, 1, 900 ) select t1.*, a.*, b.* from t1 left join t2 as a on t1.[id] = a.[id] and t1.[GroupNum] = a.[GroupNum] and t1.[Amount] = a.[SourceAmt] inner join t2 as b on t1.[id] = b.[id] and t1.[GroupNum] = b.[GroupNum] where t1.[id] = 23 CASE列进行了一些技巧,这是最终的查询

[takeIt]