mercredi 2 avril 2014

How to get the First and Last record


Vote count:

0




again. Im traying to control a set of data that have to begin with a 22 CodeId (GO) and finish with a 23 CodeId (STOP). Sometimes the data can come like the example. So, I have to check the first record to see if is CodeId = 23 then delete this record, and with the last record if CodeId = 22 Then delete the record.



DATE DriverId CodeId
**2014/03/01 13:50:00, 7168, 23 -- STOP**
2014/03/01 14:00:00, 7168, 22 -- GO
2014/03/01 14:30:00, 7168, 23 -- STOP
2014/03/01 14:40:00, 7168, 22 -- GO
2014/03/01 16:10:00, 7168, 23 -- STOP
2014/03/01 16:50:00, 7168, 22 -- GO
2014/03/01 17:20:00, 7168, 23 -- STOP
2014/03/01 18:30:00, 7168, 22 -- GO
2014/03/01 19:10:00, 7168, 23 -- STOP
**2014/03/01 14:00:00, 7168, 22 -- GO**


I wrote this code



IF (SELECT TOP(1) CodeId FROM #tbl) = 23
BEGIN
DELETE ... (Dont know what to do)
END

IF (SELECT TOP(1) CodeId FROM #tbl ORDER BY DATE DESC) = 22
BEGIN
DELETE ... (Dont know what to do)
END


I dont know the DELETE statement, can you help me please? If you consider that I have to solve this problem in a different way please let me know how. Thank you in advance.


Im using MS SQL server 2012



asked 37 secs ago






Aucun commentaire:

Enregistrer un commentaire