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
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