Dynamic update for all writes for VehiclePreviousLog

This commit is contained in:
2012-12-07 13:43:05 -05:00
parent ea2d8b4262
commit 6f28fb1621
6 changed files with 69 additions and 35 deletions
@@ -10,9 +10,11 @@ namespace MileageTraker.Web.Migrations
AddForeignKey("Log", "VehiclePreviousLogId", "Log", "LogId");
CreateIndex("Log", "VehiclePreviousLogId");
var populateSql =
@"WITH summary AS (
@"WITH summary AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY l.LogId ORDER BY l.LogId ASC, pl.EndOdometer DESC, pl.Created DESC) AS rk,
ROW_NUMBER() OVER (
PARTITION BY l.LogId
ORDER BY pl.EndOdometer DESC, pl.Created DESC) AS rk,
l.LogId, pl.LogId as PreviousLogId
FROM
[Log] as l,
@@ -22,7 +24,7 @@ namespace MileageTraker.Web.Migrations
AND ((pl.[Date] <= l.[Date]
AND pl.EndOdometer < l.EndOdometer)
OR (
pl.EndOdometer < l.EndOdometer
pl.EndOdometer = l.EndOdometer
AND pl.Created < l.Created
))
)