DECLARE @date varchar(20); set @date = '2020-05-01'; delete T_upmoneylist where (F_Pricelistfid in (select F_ID from T_PriceList where F_Roomorderid in ( select F_ID from T_RoomOrder where F_GroupNumber in ( SELECT ro1.F_GroupNumber FROM T_RoomOrder ro1 JOIN ( SELECT F_GroupNumber FROM T_RoomOrder WHERE F_EndDateTime <@date GROUP BY F_GroupNumber HAVING SUM(CASE WHEN F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*) ) ro2 ON ro1.F_GroupNumber = ro2.F_GroupNumber GROUP BY ro1.F_GroupNumber HAVING SUM(CASE WHEN ro1.F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*)) ))); --2.2上交表中的根据支付ID的记录 delete T_upmoneylist where (F_Payinfofid in (select F_ID from T_Payinfo where F_Orderid in( select F_ID from T_RoomOrder where F_GroupNumber in ( SELECT ro1.F_GroupNumber FROM T_RoomOrder ro1 JOIN ( SELECT F_GroupNumber FROM T_RoomOrder WHERE F_EndDateTime <@date GROUP BY F_GroupNumber HAVING SUM(CASE WHEN F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*) ) ro2 ON ro1.F_GroupNumber = ro2.F_GroupNumber GROUP BY ro1.F_GroupNumber HAVING SUM(CASE WHEN ro1.F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*)) ))); --3.T_payinfo delete T_PayInfo where ( F_OrderId in ( select F_ID from T_RoomOrder where F_GroupNumber in ( SELECT ro1.F_GroupNumber FROM T_RoomOrder ro1 JOIN ( SELECT F_GroupNumber FROM T_RoomOrder WHERE F_EndDateTime <@date GROUP BY F_GroupNumber HAVING SUM(CASE WHEN F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*) ) ro2 ON ro1.F_GroupNumber = ro2.F_GroupNumber GROUP BY ro1.F_GroupNumber HAVING SUM(CASE WHEN ro1.F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*)) ) ) --4.t_pricelist delete T_PriceList where F_RoomOrderID in ( select F_ID from T_RoomOrder where F_GroupNumber in ( SELECT ro1.F_GroupNumber FROM T_RoomOrder ro1 JOIN ( SELECT F_GroupNumber FROM T_RoomOrder WHERE F_EndDateTime <@date GROUP BY F_GroupNumber HAVING SUM(CASE WHEN F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*) ) ro2 ON ro1.F_GroupNumber = ro2.F_GroupNumber GROUP BY ro1.F_GroupNumber HAVING SUM(CASE WHEN ro1.F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*)) ) --5.t_orderclient delete T_OrderClient where F_OrderId in ( select F_ID from T_RoomOrder where F_GroupNumber in ( SELECT ro1.F_GroupNumber FROM T_RoomOrder ro1 JOIN ( SELECT F_GroupNumber FROM T_RoomOrder WHERE F_EndDateTime <@date GROUP BY F_GroupNumber HAVING SUM(CASE WHEN F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*) ) ro2 ON ro1.F_GroupNumber = ro2.F_GroupNumber GROUP BY ro1.F_GroupNumber HAVING SUM(CASE WHEN ro1.F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*)) ) --6.最后整个账单 delete T_RoomOrder where F_ID in ( select F_ID from T_RoomOrder where F_GroupNumber in ( SELECT ro1.F_GroupNumber FROM T_RoomOrder ro1 JOIN ( SELECT F_GroupNumber FROM T_RoomOrder WHERE F_EndDateTime <@date GROUP BY F_GroupNumber HAVING SUM(CASE WHEN F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*) ) ro2 ON ro1.F_GroupNumber = ro2.F_GroupNumber GROUP BY ro1.F_GroupNumber HAVING SUM(CASE WHEN ro1.F_Status = '已结帐' THEN 1 ELSE 0 END) = COUNT(*)) ) --外卖单 支付记录 delete T_PayInfo where ( F_ID in ( SELECT F_Payfid FROM T_OtherSinfo where F_DateTime < @date) ) --外卖单 上交记录 delete T_upmoneylist where (F_Payinfofid in (SELECT F_Payfid FROM T_OtherSinfo where F_DateTime < @date)); --外卖单消费记录 delete FROM T_OtherSinfo where F_DateTime < @date -所有的操作日志 delete FROM T_Log_Operation where F_CreateDt < @date delete FROM T_Log_RoomStatus where F_CreateDt < @date delete FROM T_Log_RoomSvrc where F_CreateDt < @date delete FROM T_Log_System where F_CreateDt < @date delete FROM T_LoginSysLog where F_LogDate < @date ---所有微信端同步的数据 delete from T_Web_ACCLogs delete from T_Web_DaySingleValue delete from T_Web_Performance delete from T_Web_ReportbySale delete from T_Web_ReportMultiple