gpt4 book ai didi

sql - 如何用相同 PolicyNumber 的非空值替换空字符串

转载 作者:行者123 更新时间:2023-12-04 22:41:35 25 4
gpt4 key购买 nike

目标是将 ClaimType 列中的空值替换为每个 CoverageTypePolicyNumber 的非空值

enter image description here

出于某种原因,我编写的 SELECT 语句没有给我理想的结果。在我指定的 WHERE 子句中:

 where  
PolicyNumber = c.PolicyNumber
and CoverageType = c.CoverageType

但为什么对于 PolicyNumber 00002 我有 CPL CoverageType 的“Occurance”?

我希望看到 Occurrence-Pollution:

enter image description here

我在这里错过了什么?

declare @ClaimType table (PolicyNumber varchar(50), QuoteID int,  CoverageType varchar(50), ClaimType varchar(100))

insert into @ClaimType values ('00001',1, 'CGL', ' '),
('00001',2, 'CGL', 'Occurrence'),
('00002',1, 'CPL', ' '),
('00002',2, 'CPL', 'Occurrence-Pollution')

select PolicyNumber,
QuoteID,
CoverageType,
ClaimType,
case when ClaimType = ' ' then (
select top 1 ClaimType
from @ClaimType c
where
PolicyNumber = c.PolicyNumber
and CoverageType = c.CoverageType
order by QuoteID desc
)
else ClaimType End as ClaimType1
from @ClaimType
go

最佳答案

这是你想要的吗?

select ct.*,
coalesce(nullif(ltrim(ClaimType), ''),
max(ClaimType) over (partition by PolicyNumber, CoverageType)
) as ClaimType_2
from @ClaimType ct;

关于sql - 如何用相同 PolicyNumber 的非空值替换空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50728026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com