はProducerCode
,およびProducerName
の2つです。しかし、エンドユーザーの不具合のためにProducerName
は、2つのCTE
の間でわずかに異なる場合があります。 をProducerCode
とProducerLocationID
でグループ化することはできますが、1つだけをProducerName
としてください(最初からCTE
)。合計を合計する方法は複数ありますが、複数の名前のうち1つの名前のみを保持する方法
これは私が今得たものである:(469956.00 + 297153.00)であるNetWrittenPremium
767109として持っているProducerCode
1321とProducerLocationID
1587必要と
2列。 WPNewBusiness
と同じですが、唯一の私はこのようにそれを必要とする 'ハリー・W Gorst共同株式会社'
として1 ProducerName
を持っている:
ここに私の完全なクエリです:
;WITH cte_TopWP --T
AS
(
SELECT ProducerCode,[Producer Location ID], Producer,
SUM(premium) as NetWrittenPremium,
SUM(CASE WHEN PolicyType = 'New Business' THEN Premium ELSE 0 END) as WPNewBusiness,
SUM(CASE WHEN PolicyType = 'Renewal' THEN Premium ELSE 0 END) as WPRenewal,
SUM(CASE WHEN PolicyType = 'Rewrite' THEN Premium ELSE 0 END) as WPRewrite,
SUM(CASE WHEN PolicyType = 'New Business' AND TransactionType = 'Policy' THEN 1 ELSE 0 END) as BindsNewBusiness,
SUM(CASE WHEN PolicyType = 'Renewal' AND TransactionType = 'Policy' THEN 1 ELSE 0 END) as BindsRenewals,
SUM(CASE WHEN PolicyType = 'Rewrite' AND TransactionType = 'Policy' THEN 1 ELSE 0 END) as BindsRewrite,
SUM(CASE WHEN PolicyType = 'New Business' AND TransactionType = 'Policy' THEN 1 ELSE 0 END) + SUM(CASE WHEN PolicyType = 'Renewal' AND TransactionType = 'Policy' THEN 1 ELSE 0 END) + SUM(CASE WHEN PolicyType = 'Rewrite' AND TransactionType = 'Policy' THEN 1 ELSE 0 END) as PolicyCount
FROM ProductionReportMetrics
WHERE EffectiveDate >=DateAdd(yy, -1, DATEADD(d, 1, EOMONTH(GETDATE()))) AND EffectiveDate <= EOMONTH(GETDATE()) AND TransactionType = 'Policy' AND CompanyLine = 'Plaza Insurance Company' AND ProducerType = 'Wholesaler'
GROUP BY ProducerCode,
[Producer Location ID],Producer
)
,
cte_Counts --C
AS
(
SELECT ProducerCode,ProducerLocationID,
COUNT (distinct ControlNo) as Submissions,
SUM(CASE WHEN QuotedPremium IS NOT NULL THEN 1 ELSE 0 END) as Quoted,
SUM(CASE WHEN Status = 'Declined' THEN 1 ELSE 0 END) as Declined
FROM ClearanceReportMetrics
WHERE EffectiveDate >=DateAdd(yy, -1, DATEADD(d, 1, EOMONTH(GETDATE()))) AND EffectiveDate <= EOMONTH(GETDATE()) AND CompanyLine = 'Plaza Insurance Company'
GROUP BY ProducerCode,
ProducerLocationID
)
SELECT
TOP 10 RANK() OVER (ORDER BY NetWrittenPremium desc) as Rank,
ROW_NUMBER() OVER (PARTITION BY t.ProducerCode, t.[Producer Location ID] ORDER BY NetWrittenPremium DESC) as num,
t.ProducerCode,
t.[Producer Location ID],
t.Producer as ProducerName ,
NetWrittenPremium,
WPNewBusiness,
WPRenewal,
WPRewrite,
PolicyCount,
Submissions,
Quoted,
BindsNewBusiness,
BindsRenewals,
BindsRewrite,
Declined
FROM cte_TopWP t INNER JOIN cte_Counts c ON t.ProducerCode=c.ProducerCode and c.ProducerLocationID=t.[Producer Location ID]
ORDER BY
NetWrittenPremium DESC
1つのCTEに信頼できるProducerNameがありますか? – jabs
まあ...最初のcteを言いましょう。 – Oleg
思想家のproducernameは2番目のCTEにありました。名前は最初のCTEにしかないので、私はそれを見つける方法を見ることができません。正しい名前を見つけるために使用できる別のテーブルがありますか? – jabs