清理 T-SQL 表
Cleaning T-SQL tables
我是 T-SQL 的新手,正在尝试对从 Excel 导入到 SQL 服务器的一些数据进行一些清理。
我进行了批量导入,将原始数据导入暂存 table,现在我想清理它。
我有以下 tables
tblRawInput
(我的分期table):
Name, Name2, Name3, Phonenumber, Group
tblPeople
:
PersonID (IDENTITY), Name, Phonenumber, GroupID
tblGroups
:
GroupID (IDENTITY), Groupname
tblAltNames
:
NameID (IDENTITY), Name, PersonID
查询应该能够将数据拆分到其他 table 中,但如果已经存在则不能创建组。
我很茫然。谁能给我一个正确方向的指示。
当我执行 SELECT INTO
时,它会创建组的多个副本。
您可以使用 not exists
子句仅插入新组:
insert into Groups
(GroupName)
select distinct Group
from tblRawInput ri
where not exists
(
select *
from Groups
where g.GroupName = ri.Group
)
在此之后,您可以像这样插入到 tblPeople
中;
insert tblPeople
(Name, GroupID)
select ri.Name
, g.GroupID
from tblRawInput ri
-- Look up GroupID in the Groups table
join Groups g
on g.GroupName = ri.Group
您可以按照相同的方式使用备用名称。
首先在这种情况下顺序很重要。首先插入到组中。
insert into tblGroups
(GroupName)
select Group
from tblRawInput ri
where not exists
(
select *
from tblGroups g
where g.GroupName = ri.Group
)
然后插人table
Insert into tblPeople(Name, Phonenumber, GroupID)
Select Name, Phonenumber, GroupID
from tblRawInput ri
join tblGroups g
on g.groupName = ri.group
where not exists
(
select *
from tblPeople p
where p.Name = ri.Name
and p.Phonenumber = ri.Phonenumber
and p.groupId = g.groupid
)
然后获取替代名称
Insert into tblAltNames (Name, Personid)
Select Distinct Name2, PersonID
from tblRawInput ri
join tblPerson p
on p.Name = ri.Name
where not exists
(
select *
from tblAltNames p
where p.Name = ri.Name2
)
当然,所有这些都应该包含在一个事务中,并且所有内容都回滚的 try catch 块是插入失败之一。
可能第二个查询应该使用输出子句来插入 personids 而不是连接。你真的没有什么好加入这里,因为名字不是唯一的。
我是 T-SQL 的新手,正在尝试对从 Excel 导入到 SQL 服务器的一些数据进行一些清理。
我进行了批量导入,将原始数据导入暂存 table,现在我想清理它。
我有以下 tables
tblRawInput
(我的分期table):Name, Name2, Name3, Phonenumber, Group
tblPeople
:PersonID (IDENTITY), Name, Phonenumber, GroupID
tblGroups
:GroupID (IDENTITY), Groupname
tblAltNames
:NameID (IDENTITY), Name, PersonID
查询应该能够将数据拆分到其他 table 中,但如果已经存在则不能创建组。
我很茫然。谁能给我一个正确方向的指示。
当我执行 SELECT INTO
时,它会创建组的多个副本。
您可以使用 not exists
子句仅插入新组:
insert into Groups
(GroupName)
select distinct Group
from tblRawInput ri
where not exists
(
select *
from Groups
where g.GroupName = ri.Group
)
在此之后,您可以像这样插入到 tblPeople
中;
insert tblPeople
(Name, GroupID)
select ri.Name
, g.GroupID
from tblRawInput ri
-- Look up GroupID in the Groups table
join Groups g
on g.GroupName = ri.Group
您可以按照相同的方式使用备用名称。
首先在这种情况下顺序很重要。首先插入到组中。
insert into tblGroups
(GroupName)
select Group
from tblRawInput ri
where not exists
(
select *
from tblGroups g
where g.GroupName = ri.Group
)
然后插人table
Insert into tblPeople(Name, Phonenumber, GroupID)
Select Name, Phonenumber, GroupID
from tblRawInput ri
join tblGroups g
on g.groupName = ri.group
where not exists
(
select *
from tblPeople p
where p.Name = ri.Name
and p.Phonenumber = ri.Phonenumber
and p.groupId = g.groupid
)
然后获取替代名称
Insert into tblAltNames (Name, Personid)
Select Distinct Name2, PersonID
from tblRawInput ri
join tblPerson p
on p.Name = ri.Name
where not exists
(
select *
from tblAltNames p
where p.Name = ri.Name2
)
当然,所有这些都应该包含在一个事务中,并且所有内容都回滚的 try catch 块是插入失败之一。
可能第二个查询应该使用输出子句来插入 personids 而不是连接。你真的没有什么好加入这里,因为名字不是唯一的。