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

后端 未结 4 1055
旧时难觅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: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

提交回复
热议问题