2017-08-14 13 views
-1

InsuredCountyの合計プレミアムとExperienceMod、SchedureMod、TerritoryMod、EffectiveModの平均を1回のクエリで返すにはどうすればよいですか?Modごとに各InsuredCountyとAverageの合計を取得する方法

select * 
from (SELECT Insured, 
      InsuredCounty, 
      PolicyNumber, 
      EffectiveDate, 
      PolicyType, 
      SUM(Premium) as Premium, 
      ExperienceMod, 
      ISNULL(ScheduleMod,0) as ScheduleMod, 
      TerritoryMod, 
      ISNULL(EffectiveMod,0) as EffectiveMod, 
      ROW_NUMBER() OVER (PARTITION BY Insured,PolicyNumber,Premium, TransactionType ORDER BY PolicyType DESC) as rid 
     FROM ProductionReportMetrics 
     WHERE EffectiveDate <= EOMONTH(GETDATE()) 
     AND CompanyLine = 'Arch Insurance Company' 
     AND Insured <>'Jerry''s Test' 
     AND TransactionType = 'Policy' 
     GROUP BY Insured, 
       InsuredCounty, 
       PolicyNumber, 
       PolicyType, 
       EffectiveDate, 
       experienceMod, 
       ScheduleMod, 
       TerritoryMod, 
       EffectiveMod, 
       Premium, 
       EffectiveDate, 
       TransactionType 
    ) b 
where rid = 1 
ORDER BY InsuredCounty, Premium desc, EffectiveDate 

現在の結果は次のようになります。

enter image description here

しかし、望ましい結果は、次のようになります。これについて

enter image description here

+0

サンプルデータと予想されるo/pを掲載してください。 –

+0

いくつかのサンプルデータを追加しました – Oleg

+0

あなたが何を求めているか全く分かりません。プレヤは画像ではなくテキストとして投稿します。 –

答えて

0

何を?

WITH CTE AS 
(
    SELECT 
    InsuredCounty AS InsuredCountry 
    , Premium  AS premium 
    , ExperienceMod AS ExperienceMod 
    , SchedureMod AS SchedureMod 
    , TerritoryMod AS TerritoryMod 
    , EffectiveMod AS EffectiveMod 
    FROM ProductionReportMetrics 
    WHERE EffectiveDate <= EOMONTH(GETDATE()) 
     AND CompanyLine = 'Arch Insurance Company' 
     AND Insured <>'Jerry''s Test' 
     AND TransactionType = 'Policy' 
) 
SELECT InsuredCountry 
    , SUM(premium) AS premium 
    , AVG(ExperienceMod) AS ExperienceMod 
    , AVG(SchedureMod) AS SchedureMod 
    , AVG(TerritoryMod) AS TerritoryMod 
    , AVG(EffectiveMod) AS EffectiveMod 
FROM CTE 
GROUP BY InsuredCountry 
ORDER BY InsuredCounty, Premium desc, EffectiveDate; 
関連する問題