2016-09-22 2 views
0

Im을 가져 오는 열 중 하나는 PolicyType이고 그 조건은 TransactionType = 'Policy'입니다. 하지만 때로는 같은 Insured은 동일한 TransactionType 값을 가질 수 있지만 다른 PolicyType 값을 가질 수 있습니다. 그래서 동일한 TransactionType과 동일한 피보험자가 있지만 PolicyType이 다른 경우 - 우선 순위를 정하고 정책 유형이 ='Rewrite' 인 레코드를 선택하고 PolicyType='New Business'으로 무시하십시오. 간단 CASE 문이어야한다하지만 난 당신의 샘플 데이터를 확인한 후 enter image description here열 이름을 기준으로 우선 순위가있는 레코드를 선택하지 않는 방법

SELECT CAST(YEAR(EffectiveDate) as CHAR(4))+'/'+ CAST(MONTH(EffectiveDate) AS VARCHAR(2)) AS YearMonth, 
     Insured, 
     PolicyNumber, 
     EffectiveDate, 
     PolicyType, 
     TransactionType, 
     SUM(Premium) as Premium, 
     ExperienceMod, 
     ISNULL(ScheduleMod,0) as ScheduleMod, 
     TerritoryMod, 
     ISNULL(EffectiveMod,0) as EffectiveMod 

FROM ProductionReportMetrics 
WHERE EffectiveDate >=DateAdd(yy, -1, DATEADD(d, 1, EOMONTH(GETDATE()))) AND EffectiveDate <= EOMONTH(GETDATE()) 
     AND CompanyLine = 'Arch Insurance Company' AND Insured <>'Jerry''s Test' AND TransactionType = 'Policy' --and PolicyNumber ='ZAWCI2517900' 
GROUP BY Insured, 
     PolicyNumber, 
     PolicyType, 
     TransactionType, 
     EffectiveDate, 
     experienceMod,ScheduleMod,TerritoryMod,EffectiveMod, Premium,EffectiveDate 
ORDER BY YEAR(EffectiveDate), (MONTH(EffectiveDate)), PolicyType 

답변

2

혼란 스러워요, 나는 policyType 열에 대한 우선 순위 rewrite > renewal > new business 가정하자.

--Create table 
create table #test (id int identity(1,1), name varchar(10), trantype varchar(10) default 'policy', policytype varchar(50)) 

--insert sample data 
insert #test (name,trantype,policytype) 
select 'abc','policy','new business' 
union all 
select 'abc','policy','rewrite' 
union all 
select 'AA','policy','new business' 
union all 
select 'BB','policy','new business' 
union all 
select 'BB','policy','rewrite' 
union all 
select 'CC','policy','rewrite' 

select * from #test 

--solution 
select * from 
(select *, row_number()over(partition by name,trantype order by policytype desc) as rid 
from #test 
) as b 
where b.rid = 1 

이 결과 :

에게 이

(1) 만 new business 거기 insured 지정에 대한했다면, 다음이 될 것입니다 :)

따라서 여기에 솔루션은, 그에 따라 코드를 변경하시기 바랍니다 온다 선택된;

(2) 어느 insured에 대해 new businessrewrite이 모두있는 경우 다시 쓰기 만 선택됩니다.

enter image description here

+0

최고 !!!!!!!!!! 대단히 감사합니다 @ 댄스 - 헨리 – Oleg