为两个表中的每个值组合获取SUM

时间:2014-05-14 07:09:52

标签: sql tsql

我有两张桌子:

1。 #Forecast_Premiums

Syndicate_Key Durg_Key  Currency_Key    Year_Of_Account Forecast_Premium  CUML_EPI_Amount
NULL            NULL      NULL          UNKNOWN              0              6
3                54        46            2000             109105            0
3                54        46            2001             128645           128646
5                47        80            2002             117829           6333
6                47        80            2002             125471           NULL
6                60        80            2003             82371            82371
10               98        215           2006             2093825          77888
10               98        215           2007             11111938         4523645

2.#Forecast_Claims

Syndicate_Key   Durg_Key    Currency_Key    Year_Of_Account Contract_Ref  Forecast_Claims   Ultimate_Profit_Comission
NULL              NULL          NULL           UNKNOWN  UNKNOWN             0                          -45
5                  47            80             2002    AB00ZZ021M12    -9991203                        NULL
5                  47            80             2002    AB00ZZ021M13    -4522                          -74412
9                  60            215            2006    AC04ZZ021M13    -2340299                       -895562
10                 98            46             2007    FAC0ZZ021M55    -2564123                       -851298

任务: 使用#Forecast_Premiums和#Forecast_Claims表编写查询以进行查找 Syndicate_Key,Durg_Key,Currency_key和Year_of_Account的每个组合的Pure Premium,累积EPI金额,Forecast_Claims和Ultimate_Profit_Comission的总金额。 注意:如果Key为NULL,则将其设置为“UNKNOWN”,如果Amount为NULL,则将其设置为0。

我的解决方案:

SELECT 
ISNULL(CAST(FP.Syndicate_key AS VARCHAR(20)), 'UNKNOWN') AS 'Syndicate_key', 
ISNULL(CAST(FP.Durg_Key AS VARCHAR(20)), 'UNKNOWN') AS 'Durg_Key',
ISNULL(CAST(FP.Currency_Key AS VARCHAR(20)), 'UNKNOWN') AS 'Currency_Key', 
fp.Year_Of_Account,
SUM(ISNULL(FP.Forecast_Premium,0)) AS 'Pure_Premium',
SUM(ISNULL(FP.CUML_EPI_Amount,0)) AS 'Cuml_Amount',
SUM(ISNULL(dc.Forecast_Claims,0)) AS 'Total_Claims',
SUM(ISNULL(dc.Ultimate_Profit_Comission,0)) AS 'Total_Comission'
FROM #FORECAST_PREMIUMS fp
left join #FORECAST_Claims dc 
ON 
(FP.Year_Of_Account = dc.Year_Of_Account AND
FP.Syndicate_Key = dc.Syndicate_Key AND
FP.Currency_Key = dc.Currency_Key AND
FP.Year_Of_Account = dc.Year_Of_Account)
GROUP BY fp.Syndicate_Key, fp.Durg_Key,fp.Currency_Key,fp.Year_Of_Account

问题: 它为一个键和年份组合返回Forecast_Claims SUM和Ultimate_Profit_Comission SUM 5 47 80 2002

此外,当它应该返回10时返回8行。

1 个答案:

答案 0 :(得分:0)

八个结果记录是正确的,因为FORECAST_PREMIUMS中有八个不同的Syndicate_Key,Durg_Key,Currency_key和Year_of_Account组合。

关于Forecast_Claims SUM:这也是正确的; 5 47 80 2002是Forecast_Claims中匹配的唯一组合。

仅:您是否应该匹配两个NULL记录?你不会这样做,因为NULL = NULL永远不会成立(只有NULL is NULL为真)。你必须做类似的事情

(
  (FP.Year_Of_Account = dc.Year_Of_Account)
  OR
  (FP.Year_Of_Account is null AND dc.Year_Of_Account is null
) AND ...

让这些记录匹配。或者:

ISNULL(FP.Year_Of_Account, -1) = ISNULL(dc.Year_Of_Account, -1) AND ...