0

我有两张表,一张有外键。我想从表 1 中删除重复项,同时更新表 2 上的键。即计算表 1 上的重复项,从重复项中保留 1 个键,并查询表 2 上的其余重复记录,用键 I 替换它们m 与表 1 保持一致。Soundex 将是最佳选择,因为并非所有名称在表 1 中都拼写正确。我有基本算法,但不知道该怎么做。帮助?

到目前为止,这就是我所拥有的:

declare @Duplicate int
declare @OriginalKey int

create table #tempTable1
(
    CourseID int,   <--- The Key I want to keep or delete 
    SchoolID int, 
    CourseName nvarchar(100),
    Category nvarchar(100),
    IsReqThisYear bit,
    yearrequired int

);

create table #tempTable2
(   
    CertID int, 
    UserID int, 
    CourseID int,   <---- Must stay updated with Table 1
    SchoolID int, 
    StartDateOfCourse datetime, 
    EndDateOfCourse datetime, 
    Type nvarchar(100),
    HrsOfClass float,
    Category nvarchar(100),
    Cost money,
    PassFail varchar(20),
    Comments nvarchar(1024),
    ExpiryDate datetime,
    Instructor nvarchar(200),
    Level nchar(10)


)

--Deletes records from Table 1 not used in Table 2--
delete from Table1
where CourseID not in (select CourseID from Table2 where CourseID is not null)

insert into #tempTable1(CourseID, SchoolID, CourseName, Category, IsReqThisYear, yearrequired)
select CourseID, SchoolID, CourseName, Category, IsReqThisYear, yearrequired from Table1

insert into #tempTable2(CertID, UserID, CourseID, SchoolID, StartDateOfCourse, EndDateOfCourse, Type, HrsOfClass,Category, Cost, PassFail, Comments, ExpiryDate, Instructor, Level)
select CertID, UserID, CourseID, SchoolID, StartDateOfCourse, EndDateOfCourse, Type, HrsOfClass,Category, Cost, PassFail, Comments, ExpiryDate, Instructor, Level from Table2

select cour.CourseName, Count(cour.CourseName) cnt from Table1 as cour
join #tempTable1 as temp on cour.CourseID = temp.CourseID
where SOUNDEX(temp.CourseName) = SOUNDEX(cour.CourseName)  <---

最后一部分不完全工作,给我一个错误

错误:选择列表中的“Table1.CourseName”列无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

更新:其中的一些名字CourseName也有数字。像有些是罗马和数字格式。也需要找到这些,但 Soundex 忽略了数字。

4

0 回答 0