I have a table, with rowid, userid, productid, and times. once I ordered the table by userid and time. It looks like:
u1, t1, p1
u1, t2, p1
u1, t3, p1
u1, t4, p2
u1, t5, p2
u1, t6, p3
u2, t7, p1
u2, t8, p1
u2, t9, p2
u2, t10,p3
.....
I want to delete those rows whose product id is same as the previous row.
therefore the final table should be :
u1, t1, p1
u1, t4, p2
u1, t6, p3
u2, t7, p1
u2, t9, p2
u2, t10,p3
.....
How can I do this in SQL? Many thanks
Simplified version
Turns out it is for SQL Server, and you can delete from the CTE directly in SQL Server (as hinted by @Royi Namir in the comments below):
Simplified live demo at sqlfiddle.
Much like this one.