2017-09-22 9 views
0

2つのテーブルのデータを結合するクエリがあります。T-SQL結合条件が両方のテーブルの値に依存する2つのテーブルを結合します。

ポリシーテーブル

PolicyID PolicyNumber PolicyStartDate 
48 FCO100009 2015-06-01 00:00:00.000 
49 FCO100009 2016-06-01 00:00:00.000 

クレーム表

ClaimID ClaimReference PolicyNumber IncidentDatetime NotificationDatetime Version 
30 287 FCO100009 2015-11-06 00:00:00.000 2015-11-27 00:00:00.000 4. Claim - Incident Date 
223 259 FCO100009 2015-11-03 00:00:00.000 2015-11-20 00:00:00.000 4. Claim - Incident Date 
1367 988 FCO100009 2016-04-15 00:00:00.000 2016-04-21 00:00:00.000 4. Claim - Incident Date 
1561 1859 FCO100009 2016-09-14 00:00:00.000 2016-09-19 00:00:00.000 4. Claim - Incident Date 
1741 443275 FCO100009 2016-05-11 00:00:00.000 2016-05-12 00:00:00.000 4. Claim - Incident Date 
1742 991 FCO100009 2016-04-20 00:00:00.000 2016-04-21 00:00:00.000 4. Claim - Incident Date 
2038 287 FCO100009 2015-11-06 00:00:00.000 2015-11-27 00:00:00.000 5. Claim - Notification Date 
3744 259 FCO100009 2015-11-03 00:00:00.000 2015-11-20 00:00:00.000 5. Claim - Notification Date 
3745 991 FCO100009 2016-04-20 00:00:00.000 2016-04-21 00:00:00.000 5. Claim - Notification Date 
4502 1859 FCO100009 2016-09-14 00:00:00.000 2016-09-19 00:00:00.000 5. Claim - Notification Date 
4639 988 FCO100009 2016-04-15 00:00:00.000 2016-04-21 00:00:00.000 5. Claim - Notification Date 
6600 443275 FCO100009 2016-05-11 00:00:00.000 2016-05-12 00:00:00.000 5. Claim - Notification Date 

異なるポリシー開始日とバージョンフィールドはどちらかであるインシデントを主張各請求項レコードの2バージョンとの方針のための2つのレコードがあります。日付または請求通知日。

私が達成しようとしているのは、PolicyNumberの2つのテーブルを結合し、結果のPolicyStartDate値をPolicy.PolicyStartDateの最大値に設定することです。ここで、PolicyStartDateは、Version = NotificationDateまたはPolicyStartDateバージョン= IncidentDateのインシデント日付よりも小さい。

ここでは、会計年度を使用していないことをご了承ください。この例では、会計年度は4月から3月です。

SELECT cds.ClaimID, 
     cds.ClaimReference, 
     p.policyID, 
     p.PolicyStartDate, 
     cds.IncidentDatetime, 
     cds.NotificationDatetime, 
     cds.[Version] 
FROM dbo.ClaimDataStaging cds 
     INNER JOIN dbo.[Policy] p 
      ON p.PolicyNumber = cds.PolicyNumber 
       AND p.PolicyStartDate < CASE WHEN cds.[Version] = '4. Claim - Incident Date' THEN cds.IncidentDatetime 
             WHEN cds.[Version] = '5. Claim - Notification Date' THEN cds.NotificationDatetime END 
WHERE cds.PolicyNumber = 'FCO100009' 
ORDER BY cds.[Version], cds.ClaimReference;   
GO 

任意のヘルプやアドバイスがはるかに高く評価:

はここで正しい答えを生成しません。私の現在のクエリです。

答えて

0

、私は必要な結果を提供するために、私のクエリを調整することができました。最終的な参考コードは次のとおりです。

SELECT DISTINCT 
     cds.ClaimID, 
     MAX(p.PolicyID) OVER (PARTITION BY cds.PolicyNumber) AS PolicyID, 
     MAX(p.PolicyStartDate) OVER (PARTITION BY cds.PolicyNumber) AS 
     PolicyStartDate, cds.ClaimKey, cds.ClaimReference, cds.ClaimStatus, 
     cds.IncidentDatetime, cds.NotificationDatetime, cds.UW_Date, 
     cds.IncidentType, cds.IncidentDescription, cds.OwnDamagePaid, 
     cds.OwnDamageReserve, cds.OwnDamageIncurred, cds.TPDamagePaid, 
     cds.TPDamageReserve, cds.TPDamageIncurred, 
     cds.BodilyInjuryPaid, cds.BodilyInjuryReserve, 
     cds.BodilyInjuryIncurred, cds.TotalPaid, cds.TotalReserve, 
     cds.EstimatedRecovery, cds.ActualRecovery, cds.TotalIncurred, cds.TotalIncurredBand, 
     CONVERT(VARCHAR(16), 'Current Period') AS TimeView, 1 AS ClaimCount, CONVERT(VARCHAR(48), [Version]) AS [Version]  
FROM dbo.ClaimDataStaging cds 
     INNER JOIN dbo.UW_Calendar u 
      ON u.UW_Date = cds.UW_Date 
     LEFT OUTER JOIN dbo.[Policy] p 
     ON p.PolicyNumber = cds.PolicyNumber 
      AND p.PolicyStartDate <= CASE 
              WHEN cds.[Version] = '4. Claim - Incident Date' THEN cds.IncidentDatetime 
              WHEN cds.[Version] = '5. Claim - Notification Date' THEN cds.NotificationDatetime 
             END; 
GO 
0

私はあなたの質問を以下のように探していると思います。それはあなたが日付のために持っていたフィルタに基づいて最大のポリシー番号を与えるでしょう。

限り、私はあなたの質問や、あなたの問題を理解されるように(重複があまりにもかつての政策をキャッチ日付<日に条件によって引き起こされる)、あなたもこのクエリを試すことができます
SELECT cds.ClaimID, 
     cds.ClaimReference, 
     p.policyID, 
     p.PolicyStartDate, 
     cds.IncidentDatetime, 
     cds.NotificationDatetime, 
     cds.[Version] 
FROM dbo.ClaimDataStaging cds 
    CROSS APPLY (
      SELECT PolicyStartDate = MAX(fp.PolicyStartDate) 
      FROM dbo.[Policy] fp 
      WHERE fp.PolicyNumber = cds.PolicyNumber 
      AND ((fp.PolicyStartDate < cds.IncidentDatetime AND cds.[Version] = '4. Claim - Incident Date') 
       OR (fp.PolicyStartDate < cds.NotificationDatetime AND cds.[Version] = '5. Claim - Notification Date')) sp 
    INNER JOIN dbo.[Policy] p ON p.PolicyNumber = cds.PolicyNumber AND p.PolicyStartDate = sp.PolicyStartDate 
WHERE cds.PolicyNumber = 'FCO100009' 
ORDER BY cds.[Version], cds.ClaimReference; 
GO 
+0

ありがとうございます。残念ながら、これは望ましい結果を返しません。期待されるリターンは、最初の2行(Claim Refs 259&287)ではPolicyStartDate = 2015-06-01、4月から3月まで実行される正しい会計年度に配置されるその他のものについては2016-06-01となります。 –

0


LEAD()関数を使用してEnddateの種類を計算します(最後の方針をキャッチするためにIS NULLを使用しましたが、必要に応じて変更できます)。

CASEを内部クエリで移動して、WHERE句でそれを繰り返さないようにしました。

SELECT cds.ClaimID, 
     cds.ClaimReference, 
     p.policyID, 
     p.PolicyStartDate, 
     cds.IncidentDatetime, 
     cds.NotificationDatetime, 
     cds.[Version] 
FROM (SELECT ClaimID, ClaimReference, IncidentDatetime,NotificationDatetime,[Version], PolicyNumber 
     , CASE WHEN [Version] = '4. Claim - Incident Date' THEN IncidentDatetime 
       WHEN [Version] = '5. Claim - Notification Date' THEN NotificationDatetime END AS CheckDate 
     FROM dbo.ClaimDataStaging) cds 
INNER JOIN (SELECT policyID, PolicyNumber, PolicyStartDate 
      , LEAD(PolicyStartDate) OVER (PARTITION BY PolicyNumber ORDER BY PolicyStartDate) AS PolicyEndDate FROM dbo.Policy) p 
      ON p.PolicyNumber = cds.PolicyNumber 
      AND p.PolicyStartDate < CheckDate 
      AND (p.PolicyEndDate IS NULL OR p.PolicyEndDate>=CheckDate) 
WHERE cds.PolicyNumber = 'FCO100009' 
ORDER BY cds.[Version], cds.ClaimReference; 

出力:受け取った提案に基づいて

+---------+----------------+----------+-------------------------+-------------------------+-------------------------+------------------------------+ 
| ClaimID | ClaimReference | policyID |  PolicyStartDate  | IncidentDatetime  | NotificationDatetime |   Version   | 
+---------+----------------+----------+-------------------------+-------------------------+-------------------------+------------------------------+ 
|  223 |   259 |  48 | 2015-06-01 00:00:00.000 | 2015-11-03 00:00:00.000 | 2015-11-20 00:00:00.000 | 4. Claim - Incident Date  | 
|  30 |   287 |  48 | 2015-06-01 00:00:00.000 | 2015-11-06 00:00:00.000 | 2015-11-27 00:00:00.000 | 4. Claim - Incident Date  | 
| 1367 |   988 |  48 | 2015-06-01 00:00:00.000 | 2016-04-15 00:00:00.000 | 2016-04-21 00:00:00.000 | 4. Claim - Incident Date  | 
| 1742 |   991 |  48 | 2015-06-01 00:00:00.000 | 2016-04-20 00:00:00.000 | 2016-04-21 00:00:00.000 | 4. Claim - Incident Date  | 
| 1561 |   1859 |  49 | 2016-06-01 00:00:00.000 | 2016-09-14 00:00:00.000 | 2016-09-19 00:00:00.000 | 4. Claim - Incident Date  | 
| 1741 |   443275 |  48 | 2015-06-01 00:00:00.000 | 2016-05-11 00:00:00.000 | 2016-05-12 00:00:00.000 | 4. Claim - Incident Date  | 
| 3744 |   259 |  48 | 2015-06-01 00:00:00.000 | 2015-11-03 00:00:00.000 | 2015-11-20 00:00:00.000 | 5. Claim - Notification Date | 
| 2038 |   287 |  48 | 2015-06-01 00:00:00.000 | 2015-11-06 00:00:00.000 | 2015-11-27 00:00:00.000 | 5. Claim - Notification Date | 
| 4639 |   988 |  48 | 2015-06-01 00:00:00.000 | 2016-04-15 00:00:00.000 | 2016-04-21 00:00:00.000 | 5. Claim - Notification Date | 
| 3745 |   991 |  48 | 2015-06-01 00:00:00.000 | 2016-04-20 00:00:00.000 | 2016-04-21 00:00:00.000 | 5. Claim - Notification Date | 
| 4502 |   1859 |  49 | 2016-06-01 00:00:00.000 | 2016-09-14 00:00:00.000 | 2016-09-19 00:00:00.000 | 5. Claim - Notification Date | 
| 6600 |   443275 |  48 | 2015-06-01 00:00:00.000 | 2016-05-11 00:00:00.000 | 2016-05-12 00:00:00.000 | 5. Claim - Notification Date | 
+---------+----------------+----------+-------------------------+-------------------------+-------------------------+------------------------------+ 
+0

提案されたソリューションをありがとう。残念ながら、これは望ましい結果を返しません。期待される収益は、最初の2行(クレーム基準259および287)については 2015-06-01、その他の場合は2016-06-01となります。 –

+0

私のようなテーブルに期待した結果を投稿すると、クエリを更新しようとする可能性があります。 – etsa

関連する問題