The problem here is the group by clause. Why are you using group by here? Also how do you handle updates & deletes happening to your table? or is it that you only perform inserts to it?the better approach will be to store a datefield in your main table say datecreated. Each date you extract the records with datecreated >lastdaysdate and perform comparison with temptable. Three conditions can occur1. A record in main table but not in temp table which means a new record so insert2. A record in both the tables which means updates3. A record not in main table but in temp which means its deleted.something likeINSERT INTO Temp (Temp_PK,other fields...)SELECT PK,other fieldsFROM table tLEFT JOIN temp tmpon tmp.Temp_PK = t.PKWHERE t.datecreated > dateadd(d,-1,dateadd(d,datediff(d,0,getdate()),0))AND tmp.Temp_PK IS NULLUPDATE tmpSET tmp.fields=t.fieldsFROM Temp tmpINNER JOIN table tON tmp.Temp_PK = t.PKWHERE t.datecreated > dateadd(d,-1,dateadd(d,datediff(d,0,getdate()),0))DELETE tmpFROM Temp tmpLEFT JOIN table tON tmp.Temp_PK = t.PKWHERE t.PK IS NULL