2011-11-22 56 views
1

我使用下面的代碼來計算給定月份(本例中爲9月份)的覆蓋範圍(汽車)的收入溢價。代碼的做法是將截至9月30日的總收入減去總收入的所有時間減去8月31日的總收入,以便僅返回9月份期間賺取的保費。完全外部聯接不返回不匹配的聯接字段值SQL Server 2005

結果是這個樣子:

eprem  coveragecode 
0.0211  AD 
277970.8291 BI 
245700.6741 COLL 
86997.5694 COMP 
85.0083  CustomParts 
848.7873  Death 
0.00   ECL 
0.00   GPIP 
692.3802  Income 
2410.5513 MED 
267670.1099 PD 
387628.504 PIP 
26.8767  PU 
11736.2762 Rental 
4304.3367 Towing 
4211.2574 UIMBI 
19804.8964 UMBI 
15145.3211 UMPD 

當我添加了溢價所有的覆蓋範圍我得到1325233.399是贏得了九月份保費正確的確切數額。

現在我想要做的是在結果中添加zipcode字段。如果我運行下面的代碼沒有在代碼中的任何地方指定的郵政編碼列,然後我得到上面的結果是正確的,但是當我添加郵編列時,我返回了成千上萬的行,這是預期的,但是當我加起來的總保費, 1323608.401而不是1325233.399這是正確的總數。我相信發生的事情是有一定的覆蓋範圍&郵政編碼組合(這是我加入了我的2派生表)上存在一個月(派生表)和其他注意事項。當我做一個正確的連接時,我得到不同數量的行,當我做一個左連接,但是當我加起來它總是等於1323608.401而不是1325233.399。我認爲我的問題的解決方案是使用完整的outter加入,從而返回所有結果(即使那些不匹配),以便我的總保費加起來爲1325233.399,但我仍然只獲得1323608.401,無論何種類型的加入我嘗試使用。有沒有人有任何想法可能會發生在這裏?任何援助將不勝感激。謝謝!

SELECT sept.eprem-aug.eprem as eprem , sept.coveragecode, zipcode 
from 

(select 
SUM(EarnedPremium) AS eprem,coveragecode, evaluationdate, zipcode      
FROM   dbo.StatRateSummary        
where evaluationdate = '09-30-2011' 
and decpagetypecode != 'x' 
GROUP BY evaluationdate ,coveragecode, zipcode 

) as sept full outer join 

(select 
SUM(EarnedPremium) AS eprem,coveragecode, evaluationdate, zipcode      
FROM   dbo.StatRateSummary        
where evaluationdate = CONVERT(DATETIME, DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,'09-30-2011'),0)), 102) 
and decpagetypecode != 'x' 
GROUP BY evaluationdate ,coveragecode, zipcode 

) as aug on sept.coveragecode = aug.coveragecode and sept.zipcode = aug.zipcode 
where sept.coveragecode is not null and sept.coveragecode <> '' 
order by coveragecode, sept.evaluationdate 
+0

什麼數據類型是EarnedPremium? – HLGEM

+0

這是一個「錢」數據類型 –

回答

1

我所要做的就是改變的第一行代碼轉換成任何空溢價值爲0

SELECT isnull(sept.eprem,0)-isnull(aug.eprem,0) as eprem , sept.coveragecode, zipcode 
from 

(select 
SUM(EarnedPremium) AS eprem,coveragecode, evaluationdate, zipcode      
FROM   dbo.StatRateSummary        
where evaluationdate = '09-30-2011' 
and decpagetypecode != 'x' 
GROUP BY evaluationdate ,coveragecode, zipcode 

) as sept full outer join 

(select 
SUM(EarnedPremium) AS eprem,coveragecode, evaluationdate, zipcode      
FROM   dbo.StatRateSummary        
where evaluationdate = CONVERT(DATETIME, DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,'09-30-2011'),0)), 102) 
and decpagetypecode != 'x' 
GROUP BY evaluationdate ,coveragecode, zipcode 

) as aug on sept.coveragecode = aug.coveragecode and sept.zipcode = aug.zipcode 
where sept.coveragecode is not null and sept.coveragecode <> '' 
order by coveragecode, sept.evaluationdate 
0

你FULL OUTER JOIN條件應該是這樣的:

SELECT sept.eprem-aug.eprem as eprem , sept.coveragecode, zipcode 
from 

(select 
SUM(EarnedPremium) AS eprem,coveragecode, evaluationdate, zipcode      
FROM   dbo.StatRateSummary        
where evaluationdate = '09-30-2011' 
and decpagetypecode != 'x' 
-- Migrated WHERE Condition 
AND COALESCE(eprem.coveragecode, '') > '' 
GROUP BY evaluationdate ,coveragecode, zipcode 

) as sept FULL OUTER join 

(select 
SUM(EarnedPremium) AS eprem,coveragecode, evaluationdate, zipcode      
FROM   dbo.StatRateSummary        
where evaluationdate = CONVERT(DATETIME, DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,'09-30-2011'),0)), 102) 
and decpagetypecode != 'x' 
GROUP BY evaluationdate ,coveragecode, zipcode 

) as aug on sept.coveragecode = aug.coveragecode and sept.zipcode = aug.zipcode 
/* 
|| See SEPT derived table for elimination of NULL and '' coverage codes 
|| where sept.coveragecode is not null and sept.coveragecode <> '' 
*/ 
order by coveragecode, sept.evaluationdate 

有一點要考慮的是,當您使用的是OUTER JOIN的WHERE條件可以導致優化重新改爲將JOIN寫爲INNER JOIN

希望這會有所幫助。

+0

嗨羅布,感謝您的答覆。我剛剛嘗試了代碼,起初我收到一條錯誤消息,說Zipcode是一個模糊的列名,所以我選擇使用sept.zipcode,然後我嘗試了aug.zipcode,然後在select語句中嘗試了它們兩個,結果仍然給我1323608.401的總保費,而不是1325233.399的金額,這是9月份的正確保費金額。我運行獲取1325233.399號碼的代碼與我原來的問題中的代碼相同,但沒有任何代碼中的任何地方提及zipcode列。 –

+0

它是如此奇怪,無論我使用什麼類型的連接,我總是得到1323608.401 ... –

+0

意識到我離開ZIP條碼在ON子句。如果刪除並重新提交查詢會怎麼樣? –