Update oldID for the records recursively

2019-07-31 07:30发布

问题:

I asked a very similar question here but I need more advance query now. The situation is, we have about 20,000 records of customers. Customers can renew and we just create a new record for it. There is no exact tracing back to which record was actually renewed. Now we have added the old ID field and I want to populate it with the exact old record. Now if it is renewed one time, that has been taken care of my old question and I can do it. The problem is if a record was renewed 3 or more time, I have to find the exact logic how the oldest record hopped to the newest one. For this we do not have a set rule but generally I am following customer start date (and the ID itself throws some light which record came first) and for now will use customer start date to populate all the records. I have included a test case here

create table #customer (
id int not null primary key identity,
cust_no varchar(12),
meter_no varchar(10),
startdate smalldatetime,
enddate smalldatetime,
oldid int null
)

insert into #customer values('AA111222','1111','2008-01-01', '2008-03-01',null) 
insert into #customer values('AA111222','1111','2009-02-01', '2009-05-01',null) 
insert into #customer values('AA111222','1111','2008-03-01', '2008-12-01',null) 
insert into #customer values('AA111222','1111','2009-05-01', '2009-07-01',null) 
insert into #customer values('AA111222','1111','2009-08-01', '2009-11-01',null) 
insert into #customer values('AA111222','1111','2010-01-01', '2010-04-01',null) 
insert into #customer values('AA111222','1111','2010-07-01', '2011-07-01',null) 
insert into #customer values('AA111222','1111','2011-03-01', '2011-07-01',null) 
insert into #customer values('AA111222','1111','2011-07-01', '2012-07-01',null) 

-- I want this result in the last column

id   cust_no      meter_no   startdate      enddate        oldid           
---- ------------ ---------- -------------- -------------- -------         
1    AA111222     1111       2008-01-01     2008-03-01     base                      
2    AA111222     1111       2009-02-01     2009-05-01     3 
3    AA111222     1111       2008-03-01     2008-12-01     1            
4    AA111222     1111       2009-05-01     2009-07-01     2            
5    AA111222     1111       2009-08-01     2009-11-01     4            
6    AA111222     1111       2010-01-01     2010-04-01     5            
7    AA111222     1111       2010-07-01     2011-07-01     6            
8    AA111222     1111       2011-03-01     2011-07-01     7            
9    AA111222     1111       2011-07-01     2012-07-01     8    

Note, different ways of doing it is appreciated so I can learn something too. So far I have looked at CTE, Join, Cursor but it would take some time for me to do it, if I can do it in the first place.

回答1:

Second Answer: You can update the old_id column with the following statement!

 Update #customer
 SET oldid =
        (Select TOP 1 c_old.id from #customer c_old
          where c_old.enddate <= #customer.startdate
          and c_old.cust_no = #customer.cust_no
          and c_old.meter_no = #customer.meter_no
          and c_old.enddate = 
                           (
                             SELECT max(c.enddate) FROM #customer c
                               where c_old.cust_no = c.cust_no
                               and c_old.meter_no = c.meter_no
                               and #customer.startdate >= c.enddate
                            ) 
          )
  from #customer
 go


回答2:

I know, that you probably don't like my answer, but I would change the design of the database and add 2 tables. It would improve speed and reduce redundant data in your database.

Table 1
Contract
contract_id  |   startdate | meter_no  |   cust_no

Table 2
Contract_detail
contract_id  | startdate   | enddate 

Here is how you could populate the tables with the old data: I assume, that every Customer has just one contract, if he has more than one entry in the customer-table, then he renewed.

If you define the column contract_id in contract as autovalue, try the following.

This inserts for every cust_no and meter_no combination one entry.

Insert into contract
(cust_no, startdate, enddate, meter_no)
Select distinct cust_no
       ,Min (startdate)
       ,Max (enddate)
       ,meter_no
from customer
Group by cust_no, meter_no
GO

Insert into contract_detail
(contract_id, cust_no, startdate, enddate)
Select co.contract_id
       ,co.cust_no
       ,cu.startdate
       ,cu.enddate 
from contract co
inner join customer cu on co.cust_no = cu.cust_no 
           and co.meter_no = cu.meter_no
GO


回答3:

The actual query that I used and that worked perfectly was. It was only possible after @chris answer. it is modified version of his.

 Update #customer
 SET oldid =
        (Select TOP 1 c_old.id from #customer c_old
          where c_old.startdate < #customer.startdate
          and c_old.cust_no = #customer.cust_no
          and c_old.meter_no = #customer.meter_no
          and c_old.id != #customer.id
          order by c_old.startdate desc
          )
  from #customer