问题是,第2行的StudentId应该为1,因为他们的姓名和父亲姓名相同。同样,由于姓名和父亲姓名相同,第4行的StudentId应该为3
我已根据我的要求修改了查询,因为当Student_name,Student dob,student父亲,email countryId和skype id相同时,我必须更新我的student_id,现在更新的查询将是
Merge into student_new_Backup
using ( select min(student_id) as stid, student_name, student_father,student_dob,email_id,skype_id,country
from student_new_Backup
group by student_name, student_father,student_dob,email_id,skype_id,country) a
on (student_new_Backup.student_name= a.student_name
and student_new_Backup.student_father=a.student_father
and student_new_Backup.student_dob=a.student_dob
unfortunately no student_id is update
and student_new_Backup.email_id=a.email_id
and student_new_Backup.skype_id=a.skype_id
and student_new_Backup.country=a.country)
when matched then
UPDATE SET student_new_Backup.student_id=a.stid
;
commit;
不幸的是,没有Student_id是最新的
答案 0 :(得分:3)
我想你想要
update t
set studentId = (select min(t2.studentId)
from t t2
where t2.name = t.name and t2.fathername = t.fathername
);
编辑:
如果您想要NULL
安全比较,则需要明确:
update t
set studentId = (select min(t2.studentId)
from t t2
where (t2.name = t.name or t2.name is null and t.name is null) and
(t2.fathername = t.fathername or t2.fathername is null and t.fathername is null)
);
答案 1 :(得分:2)
您可以使用合并进行更新
Merge into table_name
using ( select min(studentId) as stid, Name, fathername
from table_name
group by Name, fathername) a
on (table_name.Name= a.Name
and table_name.fatherName=a.fatherName)
when matched then
UPDATE SET table_name.studentId=a.stid