合并三个表的列

时间:2014-05-26 13:15:21

标签: sql sql-server sql-server-2008 sql-server-2012

我在SQL中有三个临时表:

TEMP1

UserID   Score_1
   123       100
   456       200

TEMP2

UserID   Score_2
   456       300

TEMP3

UserID   Score_3
   123       400
   789       500

我想将所有这三个表合并为一个

UserID   Score_1   Score_2   Score_3
   123       100      NULL       400
   456       200       300      NULL
   789      NULL      NULL       500

我们有什么办法可以通过SQL实现这一目标吗?我尝试使用每个连接甚至联合和连接的一些组合,但无法正确获得格式。

select P.Score_1 , U.Score_2 , A.Score_3 from #mytemp1 P join #mytemp2 U on P.UserID = U.UserID join  #mytemp3 A on P.UserID = A.UserID


select P.Score_1 , U.Score_2 , A.Score_3 from #mytemp1 P left join #mytemp2 U on P.UserID = U.UserID left join  #mytemp3 A on P.UserID = A.UserID


select P.Score_1 , U.Score_2 , A.Score_3 from #mytemp1 P right join #mytemp2 U on P.UserID = U.UserID right join  #mytemp3 A on P.UserID = A.UserID


select allData.UserID, P.Score_1 , U.Score_2 , A.Score_3 
from (select UserID from #mytemp1 union 
  select UserID from #mytemp2 union 
  select UserID from #mytemp3 ) allData 
join #mytemp1 t1 ON allData.UserID = t1.UserID 
join #mytemp2 t2 ON allData.UserID = t2.UserID 
join #mytemp3 t3 ON allData.UserID = t3.UserID

每个表可能包含不同数量的行,行数可以是偶数。

提前致谢,
乔纳森

6 个答案:

答案 0 :(得分:2)

您正在寻找的是一个完整的外部加入:

SELECT COALESCE(t1.UserID, t2.UserID, t3.UserID) as UserID, t1.Score_1, t2.Score_2, t3.Score_3
FROM Temp1 t1
FULL OUTER JOIN Temp2 t2 ON t1.UserID = t2.UserID
FULL OUTER JOIN Temp3 t3 ON COALESCE(t1.UserID, t2.UserID) = t3.UserID

SQL小提琴here

FULL OUTER JOIN和LEFT(或RIGHT)OUTER JOIN之间的区别在于,FULL join将允许两个表中的一些缺失键(在这种情况下为UserID)并且仍然返回结果,而LEFT和RIGHT变量要求至少有一个表包含所有键(如果使用LEFT或RIGHT,哪个表取决于。)

请注意在第二次连接时使用COALESCE - 这是必需的,因为我们不知道哪个表(如果有)可能与Temp3匹配。抛弃这一点需要猜测哪一个 - 如果匹配实际上只发生在另一个表中,那么你最终会得到重复的行。在这里使用COALESCE可以避免猜测。

答案 1 :(得分:1)

试试这个:

SELECT COALESCE(T1.UserID,T2.UserID,T3.UserID) as UserID,T1.Score_1,T2.Score_2,T3.Score_3
FROM Temp1 T1 FULL OUTER JOIN
     Temp2 T2 ON T1.UserID=T2.UserID FULL OUTER JOIN
     Temp3 T3 ON T1.UserID=T3.UserID

结果:

USERID  SCORE_1  SCORE_2    SCORE_3
123     100      (null)     400
456     200      300        (null)
789     (null)   (null)     500

请参阅SQL Fiddle中的结果。

如果您想将NULL值替换为0,可以使用ISNULL

SELECT COALESCE(T1.UserID,T2.UserID,T3.UserID) as UserID,ISNULL(T1.Score_1,0) as Score_1,ISNULL(T2.Score_2,0) as Score_2,ISNULL(T3.Score_3,0) as Score_3
FROM Temp1 T1 FULL OUTER JOIN
     Temp2 T2 ON T1.UserID=T2.UserID FULL OUTER JOIN
     Temp3 T3 ON T1.UserID=T3.UserID

SQL Fiddle中的示例。

答案 2 :(得分:1)

一种方法是使用full outer join

select coalesce(temp1.UserId, temp2.UserId, temp3.UserId) as UserId,
       temp1.Score_1, temp2.Score_2, temp3.Score_3
from temp1 full outer join
     temp2
     on temp2.UserId = temp1.UserId full outer join
     temp3
     on temp3.UserId = coalesce(temp1.UserId, temp2.UserId);

另一种方法从单个"驱动程序"中的所有ID开始。表然后使用left outer join

select ids.UserId, temp1.Score_1, temp2.Score_2, temp3.Score_3
from (select UserId from temp1 union 
      select UserId from temp2
      select UserId from temp3
     ) ids left outer join
     temp1
     on temp1.UserId = ids.UserId left outer join
     temp2
     on temp2.UserId = ids.UserId full outer join
     temp3
     on temp3.UserId = ids.UserId;

请注意,使用此方法,您不需要coalesce()条件中的on

答案 3 :(得分:1)

select coalesce(a.userid,b.userid,c.userid),score1,score2,score3 from #t a 
full outer join #t1 b on a.userid=b.userid 
full outer join #t2 c on a.userid=c.userid

DEMO

如果您的数据与评论中的内容相似

create table #t(userid int,score1 int)
insert into #t values(123,100),(456,200)

create table #t1(userid int,score2 int)
insert into #t1 values(456,300),(789,700)

create table #t2(userid int,score3 int)
insert into #t2 values(123,400),(789,500)


select coalesce(a.userid,b.userid,c.userid),score1,score2,score3 from #t a full outer join #t1 b on a.userid=b.userid full outer join #t2 c on coalesce(a.userid,b.userid)=c.userid

您需要coalesce

答案 4 :(得分:1)

试试这个

select allData.UserID, t1.Score_1, t2.Score_2, t3.Score_3
  from (select UserID from Temp1 union
        select UserID from Temp2 union
        select UserID from Temp3
       ) allData
left join Temp1 t1 ON allData.UserID = t1.UserID
left join Temp2 t2 ON allData.UserID = t2.UserID
left join Temp3 t3 ON allData.UserID = t3.UserID

答案 5 :(得分:0)

有很多种方法可以获得您想要的结果,但最简单的IMO是简单的UNIONGROUP

Select UserID, Max(Score_1) Score_1, Max(Score_2) Score_2, Max(Score_3) Score_3
FROM   (SELECT UserID, Score_1, NULL Score_2, NULL Score_3
        FROM Temp1
        UNION ALL
        SELECT UserID, NULL Score_1, Score_2, NULL Score_3
        FROM Temp2
        UNION ALL
        SELECT UserID, NULL Score_1, NULL Score_2, Score_3
        FROM Temp3) a
GROUP BY UserID

SQLFiddle demo