2015-10-21 67 views
0

的唯一的非最大記錄我試圖做類似下面刪除組

delete PreOrder 
where preorderid not in (
select preorderid 
from PreOrder p1 
inner join (select max(OrderDt) OrderDt, preorderid 
from preorder group by preorderid) p2 on 
p1.preorderid=p2.preorderid 
where p1.preorder in (1,2,3,4,5) 

我只是嘗試刪除記錄,如果有隻是那些受騙者。我得到了最大的工作,但如何從那些preorderid的記錄只刪除

+2

您使用的數據庫。請正確標記問題。 –

回答

1
Delete p1 
from PreOrder p1 
left join (select max(OrderDt) OrderDt, preorderid 
from preorder group by preorderid) p2 on 
p1.preorderid=p2.preorderid 
and p1.OrderDt = p2.OrderDt 
where p2.preorderid is null and p1.preorderid in (1,2,3,4,5)