0

I have a table Datawarehouse which the population went wrong. Each row has a name and an ID and for each update in the original system, a new row in the DWH table is created that should maintain the same ID and Name of the affected object.

For some reason there was generation of duplicate IDs for the same Object (same name, knowing that name is also unique object wise not table wise, same as ID, it is not a table primary key, it is just an object ID).

Knowing that I have the following fields: ID, Name, IsLatest, what I need to do now is to fix the Data. For that the pseudo code I need to use is:

 1. Select the list of rows with the same Name
 2. Select the ID where IsLatest = 1
 3. Update the rest of rows with that ID

I had issue with the update query, I was not able to select only one ID, and I was not also able to loop on different names.

4

2 に答える 2

3

これは、相関サブクエリを使用して完了することができます。

UPDATE DWH
SET id = (
    SELECT id
    FROM DataWarehouse
    WHERE DWH.name = name
        AND isLatest = 1
    )
FROM DataWarehouse DWH
于 2012-10-01T10:53:20.327 に答える
1

一時テーブルの最新のものを選択し、それを更新に使用します。

UPDATE YourTable
SET YourTable.ID = LatestTable.ID
FROM 
(
    SELECT * FROM YourTable 
    WHERE IsLatest = 1
) AS LatestTable
WHERE YourTable.[Name] = LatestTable.[Name]
于 2012-10-01T10:44:50.537 に答える