sql server - Replacing the values in sql table -
orderid, ordertime, orderitem 1, 1 am, orange 1, 2 am, apple 2, 3 am, grape 3, 2 am, apple 3, 3 am, coconut 1, 5 am, banana 1, 6 am, apple
the above original table. below table want. if order id same continuously, wwant change order time minimum time.
orderid, ordertime, orderitem 1, 1 am, orange 1, 1 am, apple 2, 3 am, grape 3, 2 am, apple 3, 2 am, coconut 1, 5 am, banana 1, 5 am, apple
there no "natural" order data in sql server if don't specify order order in no way guaranteed. need column can use define order by
. once have can like
declare @data table ( orderitemid int identity(1,1) primary key, orderid int, ordertime datetime, orderitem varchar(10)) insert @data select 1,'00:01:00','orange' union select 1,'00:01:00','apple' union select 2,'00:03:00','grape' union select 3,'00:02:00','apple' union select 3,'00:02:00','coconut' union select 1,'00:05:00','banana' union select 1,'00:05:00','apple'; cte1 ( select *, row_number() on (order orderitemid)- row_number() on (partition orderid order orderitemid) grp @data ), cte2 ( select *, min(ordertime) on (partition orderid, grp) minordertime cte1 ) update cte2 set ordertime = minordertime select orderid,convert(varchar, ordertime, 8) orderitemid, orderitem @data order orderitemid
Comments
Post a Comment