0
我有一個已遷移到新模式的數據庫。舊的數據庫沒有參照完整性,所以我需要擺脫很多重複。查找重複的行和相關記錄
我有RegisteredVehicles
表:
id | plate | state
# | 1425 | il
# | 3322 | il
和ParkingRequests
表:
id | date | registeredVehicleId (FK)
# | 2/2/12 | #
的relatoinship是一對多 - 一個登記的車輛很多請求。
以下查詢通過Plate
和State
獲取每個重複記錄,並且還輸出每個RegisteredVehicle's
ID。
select Id, Plate, [State] from RegisteredVehicles where Plate in (
select plate from RegisteredVehicles group by Plate having count(*) > 1
)
,給了我這樣的事情
Id Plate State
036d59f1-d928-40f2-b373-049122202bff 0000000 IL
615e2fab-8b43-4e42-b6f0-268038bba949 0000000
我試圖讓每個在上面的代碼塊返回的每一行車輛停車請求的數量。像這樣的東西
Id | Plate | State | # Requests
1 | 222 | IL | 2
2 | 333 | IL | 4
但我有問題使查詢比現在更復雜。這本身讓我花了很長時間纔開始工作。