-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxol ceded premium.sql
94 lines (83 loc) · 4.17 KB
/
xol ceded premium.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
declare @EvaluationPeriodKey int
set @EvaluationPeriodKey=20221031
declare @EvaluationDate date
set @EvaluationDate=(select Dateval from TMMWarehouse.dbo.DimDate where DateKey=@EvaluationPeriodKey)
if exists (select * from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id(N'tempdb..#a'))
DROP TABLE #a
if exists (select * from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id(N'tempdb..#b'))
DROP TABLE #b
select convert(varchar,TransactionDate, 112) as TransactionDate,
Sum(TotalPremiumAmount) as sourceCededWrittenPremium, Sum(TotalPremiumAmount) as sourceCededEarnedPremium
from TMMWarehouse_Staging.dbo.src_XOL_Premium_Archive xol
left join
TMMWarehouse_Staging.dbo.STG_Coverage_Mapping_XOL (nolock) cov on cov.ASLOBCode = xol.ASLOBCode
and cov.CommonLOBDescription = xol.CommonLOBDescription
WHERE TransactionDate = @EvaluationDate
group by TransactionDate
SELECT AccountingPeriodKey,
sum(CededWrittenPremiumAmount) as TargetCededWrittenPremium, sum(CededEarnedPremiumAmount) as TargetCededEarnedPremium
FROM tmmwarehouse.dbo.FactCededPremiumAccountingPeriodSnapshot f
JOIN TMMWarehouse.dbo.DimCompany c ON f.ReportingCompanyKey = c.CompanyKey
JOIN TMMWarehouse.dbo.DimASLOB a ON f.ASLOBKey = a.ASLOBKey
JOIN TMMWarehouse.dbo.DimCoverage cov on f.CoverageKey = cov.CoverageKey
JOIN TMMWarehouse.dbo.DimPolicy p on f.PolicyKey = p.PolicyKey
WHERE
f.SourceSystemKey = 1203
and
AccountingPeriodKey = @EvaluationPeriodKey
group by AccountingPeriodKey
select convert(varchar,TransactionDate, 112) as TransactionDate,
CompanyCode as ReportingCompany,
xol.ASLOBCode as ASLOB,
isnull(cov.LOBTypeCode,'Unknown') as LOBTypeCode,
Sum(TotalPremiumAmount) as sourceCededWrittenPremium, Sum(TotalPremiumAmount) as sourceCededEarnedPremium
into #a
from TMMWarehouse_Staging.dbo.src_XOL_Premium_Archive xol
left join
TMMWarehouse_Staging.dbo.STG_Coverage_Mapping_XOL (nolock) cov on cov.ASLOBCode = xol.ASLOBCode
and cov.CommonLOBDescription = xol.CommonLOBDescription
WHERE TransactionDate = @EvaluationDate
group by TransactionDate
, CompanyCode, xol.ASLOBCode, isnull(cov.LOBTypeCode,'Unknown')
--select convert(varchar,TransactionDate, 112) as TransactionDate,
-- CompanyCode as ReportingCompany,
--xol.ASLOBCode as ASLOB,
--isnull(xol.CommonLOBDescription,'Unknown') as LOBTypeCode,
--Sum(AffiliatePremiumAmount+NonAffiliatePremiumAmount) as sourceCededWrittenPremium, Sum(AffiliatePremiumAmount+NonAffiliatePremiumAmount) as sourceCededEarnedPremium
--from TMMWarehouse_Staging.dbo.src_XOL_Premium_Transaction xol
--where TransactionDate >= '2022-01-31 00:00:00.000'
--group by convert(varchar,TransactionDate, 112), CompanyCode, xol.ASLOBCode, xol.CommonLOBDescription
SELECT AccountingPeriodKey,
c.CompanyCode as ReportingCompany, a.ASLOBCode as ASLOB, cov.LOBTypeCode,
sum(CededWrittenPremiumAmount) as TargetCededWrittenPremium, sum(CededEarnedPremiumAmount) as TargetCededEarnedPremium
into #b
FROM tmmwarehouse.dbo.FactCededPremiumAccountingPeriodSnapshot f
JOIN TMMWarehouse.dbo.DimCompany c ON f.ReportingCompanyKey = c.CompanyKey
JOIN TMMWarehouse.dbo.DimASLOB a ON f.ASLOBKey = a.ASLOBKey
JOIN TMMWarehouse.dbo.DimCoverage cov on f.CoverageKey = cov.CoverageKey
JOIN TMMWarehouse.dbo.DimPolicy p on f.PolicyKey = p.PolicyKey
WHERE
f.SourceSystemKey = 1203
and
AccountingPeriodKey = @EvaluationPeriodKey
group by AccountingPeriodKey
, c.CompanyCode, a.ASLOBCode, cov.LOBTypeCode
Select
@EvaluationPeriodKey,
'XOL',
ISNULL(a.ReportingCompany, b.ReportingCompany),
ISNULL(a.ASLOB, b.ASLOB),
ISNULL(a.LobTypeCode, b.LobTypeCode),
a.sourceCededWrittenPremium as SourceCededWrittenPremium,
a.sourceCededEarnedPremium as SourceCededEarnedPremium,
b.TargetCededWrittenPremium as TargetCededWrittenPremium,
b.TargetCededEarnedPremium as TargetCededEarnedPremium
From #a a
FULL OUTER JOIN
#b b
On a.ReportingCompany = b.ReportingCompany
And a.ASLOB = b.ASLOB
and a.lobtypecode = b.lobtypecode
Where
IsNull(a.sourceCededWrittenPremium, 0.00) <> IsNull(b.TargetCededWrittenPremium, 0.00) or
IsNull(a.sourceCededEarnedPremium, 0.00) <> IsNull(b.TargetCededEarnedPremium, 0.00)