2010-09-08 61 views
0

我有一個表table1,它有以下列。group by clause

假設有像localamount這樣的記錄是20,000,30000,50000,100000然後根據我的情況,我必須根據該組的站點id刪除記錄,直到id,transid,shift id where localamount超過10,000 ...其餘記錄可用?

我的目標是從這個表中,當地量是根據網站ID超過10,0000刪除行,直到ID,TRANSID,移位ID

SiteId   varchar(10), 
    TillId   tinyint, 
    ShiftId   int, 
    TransId   int, 
    TranDate  datetime, 
    SettlementType varchar(5), 
    CreditCardNumber varchar(25), 
    ProductTypeCode varchar(10), 
    NewProductTypeCode varchar(10), 
    TransactionType int, 
    ForeignAmount money, 
    LocalAmount  money, 
    ProductCode  varchar(10) 
+1

澄清:你是說你想如果具有(SiteId,TillId,TransId,ShiftId)的所有行的LocalAmount字段的總數超過100,000,則刪除具有特定(SiteId,TillId,TransId,ShiftId)的所有記錄? – ngroot 2010-09-08 15:13:28

回答

1

林不知道我理解你說的話,但是如果沒有團隊,你難道不能做到這一點嗎?

delete from table1 where LocalAmount > 10,0000[sic] and SiteId = whatever and TillId = whatever... 

明顯走[原文]了...

0

假設你要刪除的全團,其中總和> 10000

;with cte as 
(
select sum(localamount) over 
      (partition by siteid, tillid, transid,shiftid) as l, 
* from table1 
) 
delete from cte where l>10000