sql >> データベース >  >> RDS >> Oracle

マージクエリがORA-30926を返す:ソーステーブルで安定した行のセットを取得できません

    データに重複がある可能性があります。 DISTINCT IdToUpdateがあることを保証するものではありません 他の列で使用する場合は一意です。 参照:

    CREATE TABLE #MyTable(IdToUpdate INT, LogSetIdToUpdateTo INT);
    
    INSERT INTO #MyTable VALUES (1,1), (1,2), (2,1),(3,1);
    
    SELECT DISTINCT IdToUpdate, LogSetIdToUpdateTo
    FROM #MyTable;
    

    LiveDemo

    IdToUpdateを取得します 2回。データを確認してください:

    with cte AS (
      select distinct nullLogSetId.Id as IdToUpdate,
                      knownLogSetId.LogSetId LogSetIdToUpdateTo
      from MyTable knownLogSetId 
      join MyTable nullLogSetId
        on knownLogSetId.IdentifierType = nullLogSetId.IdentifierType 
       and knownLogSetId.Identifier = nullLogSetId.Identifier 
      where 
        knownLogSetId.IdentifierType = 'DEF'
        and knownLogSetId.LogSetId >= 0 
        and nullLogSetId.LogSetId = -1
    )
    SELECT IdToUpdate, COUNT(*) AS c
    FROM cte
    GROUP BY IdToUpdate
    HAVING COUNT(*) > 1;
    

    1つの方法は、集計関数(MAX/MIN)を使用することです。 DISTINCTの代わりに :

    merge into MyTable
    using
    (
    
      select nullLogSetId.Id as IdToUpdate,
             MAX(knownLogSetId.LogSetId) AS LogSetIdToUpdateTo 
      from MyTable knownLogSetId 
      join MyTable nullLogSetId
        on knownLogSetId.IdentifierType = nullLogSetId.IdentifierType 
       and knownLogSetId.Identifier = nullLogSetId.Identifier 
      where 
        knownLogSetId.IdentifierType = 'DEF'
        and knownLogSetId.LogSetId >= 0 
        and nullLogSetId.LogSetId = -1
      GROUP BY nullLogSetId.Id
    ) on (Id = IdToUpdate)
    when matched then
    update set LogSetId = LogSetIdToUpdateTo
    



    1. InnoDBの並べ替えは本当に遅いですか?

    2. AWS上のWindowsでのShareplex入門、パート2

    3. SQLServerALLオペレーターの説明

    4. CentOSでMySQLをアップグレードする方法