Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 在两张桌子上分组_Sql Server_Tsql - Fatal编程技术网

Sql server 在两张桌子上分组

Sql server 在两张桌子上分组,sql-server,tsql,Sql Server,Tsql,我无法提出正确的质疑 如果#target中的GroupName组中的任何ID与#source中的ID匹配,则其整个组应使用其#source GroupName 例如,#target中的A具有GroupName='beta',并且ID=A存在于#source中,#target中的所有内容,其中GroupName='beta'应该从#source更新为'alpha' create table #Source ( ID varchar(55) UNIQUE, GroupName varchar(55)

我无法提出正确的质疑

如果
#target
中的
GroupName
组中的任何
ID
#source
中的
ID
匹配,则其整个组应使用其
#source GroupName

例如,
#target
中的
A
具有
GroupName='beta'
,并且
ID=A
存在于
#source
中,
#target
中的所有内容,其中
GroupName='beta'
应该从
#source
更新为
'alpha'

create table #Source
(
ID varchar(55) UNIQUE,
GroupName varchar(55))

create table #Target
(
ID varchar(55) UNIQUE,
GroupName varchar(55))

insert #Source
select 'A','alpha'
union
select 'B','alpha'
union
select 'C','alpha'

insert #Target
select 'A','beta'
union
select 'B','beta'
union
select 'C','teta'
union
select 'D','teta'
union
select 'E','zeta'
union
select 'F','zeta'

select * from #Source
select * from #Target
所以#目标应该更新到这一点

我想出来了

update trg
set GroupName = e.srcGroup
from
(
select distinct t.GroupName,s.GroupName as srcGroup 
from #Target t
inner join #Source s on t.ID = s.ID
) e
inner join #Target trg on trg.GroupName = e.GroupName