具有 ROW_Number SQL 服务器的 2 sql 之间的内部连接
Inner Join between 2 sql having ROW_Number SQL Server
我已经浏览了特定的帖子,但仍然无法解决我在两个 SQL 语句的内部联接中遇到的问题,其中有一个 Row_number
函数调用。
正在尝试从两个 table 中提取数据。我正在使用 Row_Number
来获取不同的策略,因为有很多重复值。我无法弄清楚 Inner Join 部分有什么问题。
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
错误消息示例 -
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Ch.ClaimSuffix" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Ch.AccidentDate" could not be bound.
Msg 4104, Level 16, State 1, Line 5
The multi-part identifier "CH.AccidentYear" could not be bound.
我做错了什么?它没有识别声明 table 列。
如有任何线索,我们将不胜感激。我从早上就卡在这了。
谢谢!!
您不能像那样在子查询中引用 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
许多人喜欢使用 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
row_number()
不需要select distinct
。另外,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 ;
我已经浏览了特定的帖子,但仍然无法解决我在两个 SQL 语句的内部联接中遇到的问题,其中有一个 Row_number
函数调用。
正在尝试从两个 table 中提取数据。我正在使用 Row_Number
来获取不同的策略,因为有很多重复值。我无法弄清楚 Inner Join 部分有什么问题。
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
错误消息示例 -
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Ch.ClaimSuffix" could not be bound.Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Ch.AccidentDate" could not be bound.Msg 4104, Level 16, State 1, Line 5
The multi-part identifier "CH.AccidentYear" could not be bound.
我做错了什么?它没有识别声明 table 列。
如有任何线索,我们将不胜感激。我从早上就卡在这了。
谢谢!!
您不能像那样在子查询中引用 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
许多人喜欢使用 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
row_number()
不需要select distinct
。另外,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 ;