Valid GROUP BY query doesn't work when combined with INSERT INTO on Oracle

后端 未结 4 1052
旧时难觅i
旧时难觅i 2021-02-13 22:31

I\'m trying to write an INSERT INTO that does a some DISTINCT/GROUP BY work. The query runs perfectly fine as a select statement, but will not work if it\'s wrapped into an INS

相关标签:
4条回答
  • 2021-02-13 22:34

    The problem is solved by automatically changing the value of a parameter (optimizer_features_enable). This value determines the optimizer version of the base, with 11 should not give that problem.

    0 讨论(0)
  • 2021-02-13 22:36

    Am I thinking wrong, but is not the sql below equal what you want to achieve?

    INSERT INTO MasterRecords(BatchRecordRecordID, SourceID, BatchID)
    SELECT DISTINCT RecordID, 101, 150
    FROM BatchRecords
    WHERE BatchID = 150
    ;
    
    0 讨论(0)
  • 2021-02-13 22:52

    Wonder if it's an order of execution problem... does it work if you use a CTE? CTE must first materialize thus resolve the group by...

      Insert INTO MasterRecords (BatchRecordRecordID, SourceID, BatchID)
       WITH BR AS (
        SELECT RecordID, 101 AS SourceID, 150 AS BatchID
        FROM BatchRecords
        GROUP BY RecordID, 101,150)
      Select RecordID, SourceID, BatchID FROM BR
    

    or... why the group by and where clause in the first place doesn't seem to be doing anything since recordID isn't an aggregate and isn't part of the group by...

    Insert into masterRecords (batchrecordRecordID, SourceID, BatchID) SELECT recordID, 101, 150 from batchRecords

    0 讨论(0)
  • 2021-02-13 22:53

    I arrived here trying to solve a similar situation so it seems to me that this kind of problem still appears.

    In my case, avoiding any optimizer transformation, did the trick.

    I applied a NO_QUERY_TRANSFORMATION hint to the "intoed" SELECT statement and the error disappeared.

    In the case of this question, I should rewrite it as:

    INSERT INTO MasterRecords
      (BatchRecordRecordID, SourceID, BatchID)
    SELECT /*+NO_QUERY_TRANSFORMATION*/ RecordID, SourceID, BatchID
    FROM (
        SELECT RecordID, BatchID, 101 AS SourceID
        FROM BatchRecords
        WHERE BatchID = 150
        GROUP BY RecordID, BatchID
    ) BR
    
    0 讨论(0)
提交回复
热议问题