具有ROW_Number SQL Server的2个SQL之间的内部联接

时间:2018-10-08 20:10:21

标签: sql sql-server inner-join row-number

我已经遍历了特定的帖子,但是仍然无法解决我的两个带有Row_number函数调用的SQL语句的内部联接的问题。

尝试从两个表中提取数据。我使用Row_Number来获得不同的策略,因为有很多重复的值。我无法弄清楚“内部联接”部分出了什么问题。

Select * 
from 
    (Select Distinct
         PolicyReference as IRIS_Policy_Ref ,
         REPLACE(SUBSTRING(Ch.ClaimSuffix,3,4),'-','') as Claims_Seq,
         CH.AccidentDate as Loss_Date,
         CH.AccidentYear as Loss_Year,
         CH.ClaimCreatedDate as Claim_Advised_Date,
         CH.NoticeDescription as Loss_Description,
         NULL as Conv_Claim_No,
         NULL as CHI,     
         NULL as Manual, 
         BrokerRef as Broker_Code,                
         Null as Current_ACR,
         Null as Current_IBNR,
         Source ='DCT',
         ROW_NUMBER() OVER(PARTITION BY PolicyReference ORDER BY TransactionDate DESC) RowNum 
     from 
         dbo.Policy) PM 
INNER JOIN 
    dbo.Claims CH ON Ch.PolicyReference = PM.PolicyReference
where 
    PM.RowNum = 1 

错误消息示例-

  

信息4104,第16级,状态1,第3行
  不能绑定多部分标识符“ Ch.ClaimSuffix”。

     

信息4104,第16级,状态1,第3行
  多部分标识符“ Ch.AccidentDate”无法绑定。

     

Msg 4104,第16级,状态1,第5行
  不能绑定多部分标识符“ CH.AccidentYear”。

我在做什么错?它无法识别索赔表列。

任何线索都将不胜感激。从早上开始,我就被困在这个地方。

谢谢!!

3 个答案:

答案 0 :(得分:2)

您不能像在子查询中那样引用Claims,因为它尚未引入。为什么不这样将联接移到主查询?

Select * 
from 
(
    Select 
        PolicyReference as IRIS_Policy_Ref ,
        REPLACE(SUBSTRING(Ch.ClaimSuffix,3,4),'-','') as Claims_Seq,
        CH.AccidentDate as Loss_Date,
        CH.AccidentYear as Loss_Year,
        CH.ClaimCreatedDate as Claim_Advised_Date,
        CH.NoticeDescription as Loss_Description,
        NULL  as Conv_Claim_No,
        NULL as CHI,     
        NULL as Manual, 
        BrokerRef as Broker_Code,                
        Null as Current_ACR,
        Null as Current_IBNR,
        Source ='DCT',
        ROW_NUMBER() OVER(PARTITION BY PolicyReference ORDER BY TransactionDate DESC) RowNum 
    from dbo.Policy P
    INNER JOIN dbo.Claims CH ON Ch.PolicyReference = P.PolicyReference
) PM 
where PM.RowNum = 1 

答案 1 :(得分:0)

许多人喜欢使用CTE:

with table_with_rowsnums as (
Select * from (Select Distinct
PolicyReference as IRIS_Policy_Ref ,
REPLACE(SUBSTRING(Ch.ClaimSuffix,3,4),'-','') as Claims_Seq,
CH.AccidentDate as Loss_Date,
CH.AccidentYear as Loss_Year,
CH.ClaimCreatedDate as Claim_Advised_Date,
CH.NoticeDescription as Loss_Description,
NULL  as Conv_Claim_No,
NULL as CHI,     
NULL as Manual, 
BrokerRef as Broker_Code,                
Null as Current_ACR,
Null as Current_IBNR,
Source ='DCT',
ROW_NUMBER() OVER(PARTITION BY PolicyReference ORDER BY TransactionDate 
DESC) RowNum 
from dbo.Policy ) PM 
INNER JOIN dbo.Claims CH ON Ch.PolicyReference = PM.PolicyReference)
select * from  table_with_rowsnums where rownum=1

答案 2 :(得分:0)

无需将select distinctrow_number()一起使用。另外,CH没有参考。您需要在子查询中执行JOIN

select * 
from (Select pm.PolicyReference as IRIS_Policy_Ref ,
             REPLACE(SUBSTRING(Ch.ClaimSuffix, 3, 4), '-', '') as Claims_Seq,
             CH.AccidentDate as Loss_Date,
             CH.AccidentYear as Loss_Year,
             CH.ClaimCreatedDate as Claim_Advised_Date,
             CH.NoticeDescription as Loss_Description,
             NULL as Conv_Claim_No,
             NULL as CHI,     
             NULL as Manual, 
             BrokerRef as Broker_Code,                
             Null as Current_ACR,
             Null as Current_IBNR,
             Source ='DCT',
             ROW_NUMBER() OVER (PARTITION BY pm.PolicyReference ORDER BY pm.TransactionDate DESC) RowNum 
         from dbo.Policy p JOIN
              dbo.Claims CH
              ON Ch.PolicyReference = PM.PolicyReference
        ) PM 
where PM.RowNum = 1 ;