简体   繁体   中英

SQL table update all but the most recent record for every Id

Considering the below table where is have 4 difference customers with more than 1 orders each. I need to updated the column XYZ to 1 for each customer depending on the date created column.

Update the table1 set xyz = 1

condition only the latest ( date created ) order should have XYZ value as 0

Customerid  Orderid     Date Created       XYZ 
12193438    13393354    09/08/2011 16:35    0
12193438    13384318    05/08/2011 14:08    0
12193438    13384458    08/08/2011 14:01    0
21801966    13379456    06/08/2011 12:59    0
21801966    13380639    06/08/2011 16:42    0
21971567    13385322    22/08/2011 18:00    0
21971567    13380200    09/08/2011 21:03    0
66697824    13389263    07/08/2011 13:44    0
66697824    13380162    08/08/2011 15:48    0

IT should look as below

Customerid  Orderid       Date Created     XYZ
12193438    13393354    09/08/2011 16:35    0
12193438    13384318    05/08/2011 14:08    1
12193438    13384458    08/08/2011 14:01    1
21801966    13379456    06/08/2011 12:59    1
21801966    13380639    06/08/2011 16:42    0
21971567    13385322    22/08/2011 18:00    0
21971567    13380200    09/08/2011 21:03    1
66697824    13389263    07/08/2011 13:44    1
66697824    13380162    08/08/2011 15:48    0
UPDATE thetable tt
SET xyz = 1
WHERE EXISTS (
  SELECT * FROM thetable ex
  WHERE ex.customerId = tt.customerId
  AND ex.dateCreated > tt.dateCreated
  );

You can do this by using update and join . This version uses aggregation to find the right rows to update:

update xyz join
       (select customerid, max(datecreated) as maxdc
        from xyz
        group by customerid
       ) cd
       on xyz.customerid = cd.customerid and xyz.datecreated < cd.maxdc
    set xyz.XYZ = 1;

To maintain the data over time, you might consider consider using triggers.

If you want to set both 0 and 1 at the same time (so not assume that everything starts as 0, you an do:

update xyz join
       (select customerid, max(datecreated) as maxdc
        from xyz
        group by customerid
       ) cd
       on xyz.customerid = cd.customerid 
    set xyz.XYZ = xyz.datecreated < cd.maxdc;

so for each customer ,latest ( date created ) order should have XYZ value as 0 and other has 1

Update the table1 
set xyz = 1 where OrderID not in (select OrderID from (select * from table1 order by DateCreated desc)a group by CustomerID,OrderID)b

Assumption - XYZ default value is 0

Update the table1 set xyz = 1 where datecreated < '<your date you want>'

this will set 1 to all xyz for which datecreated is less than what you specify. PS: datecreated should be an index for it to work fast.

This involves shifitng the data into table2 and then updating table2 to populate the XYZ column.

select *,RANK()over(partition by customerid order by date_created desc) as dateorder into table2
from table1 
update table2 set XYZ = 1 where dateorder not like 1

It might seem strange that table1 itself is not updated...the RANK function is an analysis processing tool which doesn't necesarily work without the creation of an analysis table. This is mainly based in the fact that table1 is busy receiving the order data! Here, table1 (transaction table) can carry on recieving data (transacting) and, at less busy times, data added since the last analysis can be shifted out of it and onto table2 (analysis table):

DECLARE @customerid INT -- used for file name

DECLARE db_cursor CURSOR FOR
SELECT distinct customerid FROM orders

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @customerid

WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE Orders set XYZ = 1 where Orderid <> (select top 1 Orderid from orders where Customerid = @customerid order by DateCreated desc) and Customerid = @customerid

   FETCH NEXT FROM db_cursor INTO @customerid   

END

CLOSE db_cursor
DEALLOCATE db_cursor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM