1

好的,我正在处理一个有 4 列的数据库表,比如说名字、中间名、姓氏和组 id。我想根据人们具有相同first且不last names考虑他们的middle name. 如果有新条目进入,我还想为该条目提供正确的组 ID。这是一个结果示例:

----------------------------------------------------------
|  First_Name |  Middle_Name  |  Last_Name  |  Group_ID  |
----------------------------------------------------------
|  Jon        |  Jacob        |  Schmidt    |   1        |
----------------------------------------------------------
|  William    |  B.           |  Schmidt    |   1        |
----------------------------------------------------------
|  Sally      |  Anne         |  Johnson    |   2        |
----------------------------------------------------------

我不确定这是否属于计算列、某种连接或不那么晦涩的东西的管辖范围,请帮忙!

4

1 回答 1

1

如果您只需要枚举查询中的组,那么row_number()将为您工作:

declare @Names table ( First_Name varchar(10), Middle_Name varchar(10), Last_Name varchar(10))
insert into @Names
    select 'Jon', 'Jacob', 'Schmidt' union all
    select 'William', 'B.', 'Schmidt' union all
    select 'Sally', 'Anne', 'Johnson' union all
    select 'Jon', 'Two', 'Schmidt'

;with Groups (First_Name, Last_Name, Group_ID) as
(   select  First_Name, Last_Name, row_number()over(order by Last_Name)
    from    @Names
    group
    by      First_Name, Last_Name
)
select  n.First_Name, n.Middle_Name, n.Last_Name, g.Group_Id
from    @Names n
join    Groups g on
        n.First_Name = g.First_Name and
        n.Last_Name = g.Last_Name;

请注意,Group_ID 值会随着新 nameGroups 的引入而改变。

如果您想分配和保留一个 Group_ID,那么我建议创建一个辅助表并在那里分配 Group_ID。

通过将映射存储在 @Names 表之外,您允许用户更改他们的名称,而不必担心重新评估组分配。它还允许您在不重新分配名称的情况下修改分组逻辑。您还可以将足够相似的值映射到相同的分组(John、Jon、Jonny)。

Group_ID 由 First_Name 和 Last_Name 组成。所以,就这样存放吧。

declare @Names table ( First_Name varchar(10), Middle_Name varchar(10), Last_Name varchar(10))
insert into @Names
    select 'Jon', 'Jacob', 'Schmidt' union all
    select 'William', 'B.', 'Schmidt' union all
    select 'Sally', 'Anne', 'Johnson' union all
    select 'Jon', 'Two', 'Schmidt'

declare @NameGroup table (Group_Id int identity(1,1), First_Name varchar(10), Last_Name varchar(10) unique(First_Name, Last_Name));
insert into @NameGroup (First_Name, Last_Name)
    select 'Jon', 'Schmidt' union all
    select 'Sally', 'Johnson';


declare @Group_ID int;

declare @First_Name varchar(10), 
        @Middle_Name varchar(10),
        @Last_Name varchar(10)

select  @First_Name = 'Jon',
        @Middle_Name = 'X',
        @Last_Name = 'Schmidt'

--be sure the Id has already been assigned
insert into @NameGroup
    select @First_Name, @Last_Name
    where  not exists(select 1 from @NameGroup where First_Name = @First_Name and Last_Name = @Last_Name)

--resolve the id
select  @Group_ID = Group_ID 
from    @NameGroup 
where   First_Name = @First_Name and 
        Last_Name = @Last_Name;

--store the name
insert into @Names (First_Name, Middle_Name, Last_Name)
    values(@First_Name, @Middle_Name, @Last_Name);


select  n.First_Name, n.Middle_Name, n.Last_Name, ng.Group_Id
from    @Names n
join    @NameGroup ng on
        n.First_Name = ng.First_Name and
        n.Last_Name = ng.Last_Name;
于 2013-06-17T22:00:46.857 回答