我有3种方案要实现。
情况1-当IM.Code = IME.Code和IM.Effective_st_dt = IME.effective_st_dt和IM.Effective_end_dt = IME.Effective-end_date时
行动-在这种情况下,我需要使用currentMrp和currentCp更新IM。
情况2-当IM.Code = IME.Code和IM.Effective_st_dt!= IME.effective_st_dt和IM.Effective_end_dt = IME.Effective-end_date
动作-1。)从IM中选择最近的Effective_st_dt记录并使用(IME.Effective_st_dt)-1更新Effective_end_date
2.)然后从IME插入一条新记录,该记录具有新的effective_st_dt,该记录lastMRP和lastcp是上一条记录的currentmrp和Currentcp,并在1中更新
情况3-当IM.Code = IME.Code和IM.Effective_st_dt!= IME.effective_st_dt和IM.Effective_end_dt!= IME.Effective-end_date
动作-1。)从IM中选择最近的Effective_st_dt记录并使用(IME.Effective_st_dt)-1更新Effective_end_date
2.)然后从IME插入一条新记录,该记录具有新的effective_st_dt,该记录lastMRP和lastcp是上一条记录的currentmrp和Currentcp,并在1中更新
这是我表的脚本
Create Table IM
(
ID int idenetity (1,1)
,Code varchar(100)
,CurrentMrp float
,CurrentCP float
,lastMrp float
,lastCp float
, effective_st_dt date
,effective_end_dt date
)
Create table IME
(
Code varchar(100)
,CurrentMrp float
,CurrentCP float
,Effective_st_dt date
,Effective_end_dt date
)
insert into IM (code, currentMrp, currentCp, lastMRP, lastCP, effective_st_dt, effective_end_dt)
Select
'CA123', 10.12, 5.0, 8.20, 4, '2014-05-01', '2014-05-31'
union
Select 'CA123',15.0,5.0,10.12,8.20,'2014-06-01','2014-08-31'
union
Select 'CA121',50.0,15.0,45.0,25.0,'2014-04-01','2014-05-31'
union
Select 'CA121',75.0,25.0,50.0,15.0,'2014-06-01','2014-06-30'
union
Select 'CA131',53.0,12.0,35.0,10.0,'2014-05-01','2014-05-31'
union
Select 'CA131',60.0,15.0,53.0,12.0,'2014-06-01','2014-08-31'
Insert into IME (code,effective_st_dt,effective_end_dt)
Select ('CA123',20.0,5.0,'2014-06-01','2014-08-31')
union
Select ('CA123',25.0,6.0,'2014-06-20','2014-08-31')
union
Select ('CA123',35.0,7.0,'2014-07-15','2015-03-31')
请帮我解决这个问题
感谢您的帮助
在这种特殊情况下,最好使用左联接而不是内联接,否则不等于将不会为您提供正确的结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句