我有一个基表per_name:
start_date end_date type name person_id
01-jan-2016 02-Feb-2016 A abc 1
03-Feb-2016 31-Dec-412 E abc 1
Per_leg_data
start_date end_date leg_code person_id
31-Nov-2015 03-Mar-2016 SG 1
04-Mar-2016 31-Dec-4712 CG 1
现在我想要的是每当leg_data的名称<> min(start_date)的min(start_date),那么per_leg_data的第一行应该被per_name min(start_date)替换
类似的东西:
If min(name.start_date) <> min(leg.start_date)
then
update per_leg_data
set start_date = (select distinct min(start_date)
from per_name pn
where pn.person_id=person_id)
但是这会更新date_start的per_leg_data的所有行
答案 0 :(得分:2)
这样的东西就是你所需要的。如果我在记事本++中写了一个拼写错误或其他什么,请道歉。
with T as --This table contain only the min datetime of each person.
(
select n.person_id, min(n.start_date) personMinDate, min(l.start_date) legMinDate
from per_name n
join per_leg_data l on l.person_id = n.person_id
group by n.person_id
)
update leg
-- this part replace your leg start date with your person min date
set leg.start_date = T.personMinDate
from per_leg_data leg
-- this join will give you only the correct row of the leg record.
join T on T.person_id = leg.person_id and T.legMinDate = leg.start_date
-- this filter make this is only executed when necessary.
where leg.start_date <> T.personMinDate;
答案 1 :(得分:0)
iF表中没有主键,数据按start_date(升序)排序,然后更新查询需要重写为
如果min(name.start_date)&lt;&gt;分钟(leg.start_date)
然后
更新per_leg_data
set start_date =(选择distinct min(start_date)
来自per_name pn
其中pn.person_id = person_id)
和pn.start_date =(从per_name中选择distinct min(start_date))