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
任意のヘルプやアドバイスがはるかに高く評価:
はここで正しい答えを生成しません。私の現在のクエリです。
ありがとうございます。残念ながら、これは望ましい結果を返しません。期待されるリターンは、最初の2行(Claim Refs 259&287)ではPolicyStartDate = 2015-06-01、4月から3月まで実行される正しい会計年度に配置されるその他のものについては2016-06-01となります。 –