SQL联接请帮我下面

时间:2014-09-02 13:36:28

标签: sql-server sql-server-2008 tsql

我有数据在下面的两个表中:

enter image description here

我需要第三个表输出。我使用了连接,但它不起作用。我也使用了等级。

SELECT 
   a.requestinstancelogid AS requestinstancelogidIn, 
   a.requestinstanceid AS InRequestInstance, 
   a.requeststatuscdid AS Inrequeststatuscdid, 
   a.Addnldocsin, 
   b.requestinstancelogid AS requestinstancelogidOut, 
   b.requestinstanceid AS OutRequestInstance, 
   b.requeststatuscdid AS Outrequeststatuscdid, 
   b.AddnldocsOut, 
   RANK() OVER (PARTITION BY a.requestinstanceid ORDER BY a.requestinstancelogid) AS one, 
   RANK() OVER (PARTITION BY b.requestinstanceid ORDER BY b.requestinstancelogid) AS two
INTO 
   #tmp_MultipleAddnlDocsLender
FROM 
   #tmp_addnldocsin a
LEFT JOIN 
   #tmp_addnldocsout b ON a.requestinstanceid = b.requestinstanceid
WHERE 
   a.requestinstanceid = 164944 

此查询返回6条记录。

4 个答案:

答案 0 :(得分:2)

我能看到你能产生这些结果的唯一方法如下:

;WITH Table1CTE AS
(
    SELECT *,
           ROW_NUMBER()OVER(PARTITION BY requestinstanceidin ORDER BY requestinstancelogid) AS rn
    FROM   Table1
), Table2CTE AS
(
    SELECT *,
           ROW_NUMBER()OVER(PARTITION BY requestinstanceidout ORDER BY requestinstancelogid) AS rn
    FROM   Table2
)
SELECT * 
FROM   Table1CTE T1
       LEFT JOIN Table2CTE T2
           ON T1.requestinstanceidin = T2.requestinstanceidout 
              AND T1.rn= T2.rn

答案 1 :(得分:1)

enter image description here

据我所知,两者中的同一列是requestinstanceidout(很难读出来,请在下次发布文字或代码)

根据上述观点是正确的,你可以这样:

SELECT 
*
FROM 
table1 a
LEFT JOIN table 2 b
ON a.requestinstanceidin = b.requestinstanceidout

希望这会对你有帮助!

答案 2 :(得分:0)

我认为您需要交叉/外部申请 像这样

SELECT    
    a.requestinstancelogid AS requestinstancelogidIn, 
    a.requestinstanceid AS InRequestInstance, 
    a.requeststatuscdid AS Inrequeststatuscdid,
    a.Addnldocsin, 
    q.requestinstancelogid AS requestinstancelogidOut, 
    q.requestinstanceid AS OutRequestInstance, 
    q.requeststatuscdid AS Outrequeststatuscdid, 
    q.AddnldocsOut 
FROM #tmp_addnldocsin a
OUTER APPLY (SELECT TOP 1 
    b.requestinstancelogid AS requestinstancelogidOut, 
    b.requestinstanceid AS OutRequestInstance, 
    b.requeststatuscdid AS Outrequeststatuscdid, 
    b.AddnldocsOut 
    FROM #tmp_addnldocsout b 
    WHERE a.requestinstanceidin = b.requestinstanceidout 
    ORDER BY AddnIdocsOut DESC) q
WHERE 
   a.requestinstanceid = 164944 

答案 3 :(得分:0)

我相信这就是你要找的东西:

declare @request_instance_log_id_in table (
 [log]       [int]
 , [in]      [int]
 , [status]  [int]
 , [created] [datetime] default current_timestamp);
insert into @request_instance_log_id_in
        ([log],[in],[status])
values      (424593,164944,110),
        (424594,164944,110),
        (424595,164944,110);
declare @request_instance_log_id_out table (
[log]       [int]
, [out]     [int]
, [status]  [int]
, [created] [datetime] default current_timestamp);
insert into @request_instance_log_id_out
        ([log],[out],[status])
values      (424593,164944,112),
        (424594,164944,112);
select [request_instance_log_id_in].[log]        as [request_instance_log_id_in_log]
   , [request_instance_log_id_in].[in]       as [request_instance_log_id_in_in]
   , [request_instance_log_id_in].[status]   as [request_instance_log_id_in_status]
   , [request_instance_log_id_in].[created]  as [request_instance_log_id_in_created]
   , [request_instance_log_id_out].[log]     as [request_instance_log_id_out_log]
   , [request_instance_log_id_out].[out]     as [request_instance_log_id_out_out]
   , [request_instance_log_id_out].[status]  as [request_instance_log_id_out_status]
   , [request_instance_log_id_out].[created] as [request_instance_log_id_out_created]
from   @request_instance_log_id_in as [request_instance_log_id_in]
   left join @request_instance_log_id_out as [request_instance_log_id_out]
          on [request_instance_log_id_out].[log] = [request_instance_log_id_in].[log]; 

request_instance_log_id_in_log request_instance_log_id_in_in request_instance_log_id_in_status request_instance_log_id_in_created request_instance_log_id_out_log request_instance_log_id_out_out request_instance_log_id_out_status request_instance_log_id_out_created 424593 164944 110 2014-09-02 08:55:01.107 424593 164944 112 2014-09-02 08:55:01.107 424594 164944 110 2014-09-02 08:55:01.107 424594 164944 112 2014-09-02 08:55:01.107 424595 164944 110 2014-09-02 08:55:01.107 NULL NULL NULL NULL