我使用SQL Server并尝试修改以下查询,特别是case
语句的不同方法。
以下查询正常运行,并在TransactionalCurrency
为USD
时返回正确的结果。
但我正在寻找修改。
例如,对于特定的MasterPolicyNumber,当交易货币不是USD 时,我想要加入MasterPolicyNumber上的另一个表(TransDetails)并使用其值& #39;更新层'此查询的列。基本上复制了“更新图层”的价值。仅当TransactionalCurrency不是USD时,TransDetails并将其粘贴在下面的结果中。
with PolicyDtls as
( select
MasterPolicyNumber,
PolicyNumber,
NewRenewal,
TransactionalCurrency,
LimitUSD,
AttachmentType,
ISNULL(AttachmentUSD, 0) + ISNULL(SIR_USD,0) + ISNULL(DeductibleUSD,0) as AttachmentUSD
from Policy_Test
)
select
ref.Region,
ref.MasterPolicyNumber,
previous.MasterPolicyNumber as PriorMasterPolicyNumber,
curr.TransactionalCurrency,
curr.LimitUSD,
previous.LimitUSD as prevLimitUSD,
curr.AttachmentUSD,
previous.AttachmentUSD as prevAttachUSD,
case when curr.LimitUSD = previous.LimitUSD and curr.AttachmentUSD = previous.AttachmentUSD then 'Renewed Same Layer'
when (curr.LimitUSD <> previous.LimitUSD OR curr.AttachmentUSD <> previous.AttachmentUSD )
and previous.AttachmentUSD IS NOT NULL
then 'Renewed Different Layer'
else 'Renewed Unknown'
end as 'Renewal Layer',
curr.AttachmentType
from Actuarial.Rptng.Renewal_XREF ref
left join PolicyDtls curr on curr.MasterPolicyNumber = ref.MasterPolicyNumber
left join PolicyDtls previous on previous.MasterPolicyNumber = ref.Prior_MasterPolicyNumber
where
curr.NewRenewal = 'Renewal'
and curr.MasterPolicyNumber = '49-NPR-000013-02'
order by curr.PolicyNumber
仅使用上述查询的示例数据:
--===== If the test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable') IS NOT NULL
DROP TABLE #mytable
--===== Create the test table with
CREATE TABLE #mytable
(
Region nvarchar(300),
MasterPolicyNumber nvarchar(300),
PriorPolicyNumber nvarchar(300),
TransactionalCurrency nvarchar(100),
LimitUSD float,
prevLimitUSD float,
AttachmentUSD float,
prevAttachUSD float,
RenewedLayer nvarchar(300),
AttachmentType nvarchar(100)
)
SET DATEFORMAT DMY
--===== Insert the test data into the test table
INSERT INTO #mytable (Region, MasterPolicyNumber, PriorPolicyNumber, TransactionalCurrency, LimitUSD, prevLimitUSD, AttachmentUSD, prevAttachUSD, RenewedLayer, AttachmentType)
SELECT 'EUR', '47-ACA-000001-02', '47-ACA-000001-01', 'EUR', '7105.8', '6218.6', '32763', '23273', 'Renewed Differed Layer', 'Excess'
select *from #mytable
TransDetails表的示例数据
--===== If the test table already exists, drop it
IF OBJECT_ID('TempDB..#TransDetails') IS NOT NULL
DROP TABLE #TransDetails
--===== Create the test table with
CREATE TABLE #TransDetails
(
Region nvarchar(300),
MasterPolicyNumber nvarchar(300),
PriorPolicyNumber nvarchar(300),
TransactionalCurrency nvarchar(100),
LimitTrans float,
prevLimitTrans float,
AttachmentTrans float,
prevAttachTrans float,
RenewedLayer nvarchar(300),
AttachmentType nvarchar(100)
)
SET DATEFORMAT DMY
--===== Insert the test data into the test table
INSERT INTO #TransDetails(Region, MasterPolicyNumber, PriorPolicyNumber, TransactionalCurrency, LimitTrans, prevLimitTrans, AttachmentTrans, prevAttachTrans, RenewedLayer, AttachmentType)
SELECT 'EUR', '47-ACA-000001-02', '47-ACA-000001-01', 'EUR', '8000', '8000', '3000', '3000', 'Renewed Same Layer', 'Excess'
select *from #TransDetails
预期结果
--===== If the test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable') IS NOT NULL
DROP TABLE #mytable
--===== Create the test table with
CREATE TABLE #mytable
(
Region nvarchar(300),
MasterPolicyNumber nvarchar(300),
PriorPolicyNumber nvarchar(300),
TransactionalCurrency nvarchar(100),
LimitUSD float,
prevLimitUSD float,
AttachmentUSD float,
prevAttachUSD float,
RenewedLayer nvarchar(300),
AttachmentType nvarchar(100)
)
SET DATEFORMAT DMY
--===== Insert the test data into the test table
INSERT INTO #mytable (Region, MasterPolicyNumber, PriorPolicyNumber, TransactionalCurrency, LimitUSD, prevLimitUSD, AttachmentUSD, prevAttachUSD, RenewedLayer, AttachmentType)
SELECT 'EUR', '47-ACA-000001-02', '47-ACA-000001-01', 'EUR', '7105.8', '6218.6', '32763', '23273', 'Renewed Same Layer', 'Excess'
select *from #mytable
答案 0 :(得分:1)
只要可以创建JOIN到TransDetail
以便它不会创建重复项,您只需将它作为OUTER JOIN添加到FROM子句中,并对现有的CASE表达式执行以下操作:
case
when curr.TransactionalCurrency<>'USD' then TransDetail.RenewedLayer
when curr.LimitUSD = previous.LimitUSD and curr.AttachmentUSD = previous.AttachmentUSD then 'Renewed Same Layer'
when (curr.LimitUSD <> previous.LimitUSD OR curr.AttachmentUSD <> previous.AttachmentUSD )
and previous.AttachmentUSD IS NOT NULL
then 'Renewed Different Layer'
else 'Renewed Unknown'
end as 'Renewal Layer',