--测试数据
declare @表 table(模号 varchar(10),货品编号 varchar(10))
insert @表
select '001','A001'
union all select '001','A002'
union all select '001','A003'
union all select '002','A001'
union all select '002','D001'
union all select '002','F001'
--下面是几种查询方法:
--取最小一个
select 模号,货品编号=max(货品编号)
from @表 group by 模号
--取最大一个
select 模号,货品编号=min(货品编号)
from @表 group by 模号
--取第一个
select 模号
,货品编号=(select top 1 货品编号 from @表 where 模号=a.模号)
from(select distinct 模号 from @表) a
--随机取一个
select 模号
,货品编号=(select top 1 货品编号 from @表 where 模号=a.模号 order by newid())
from(select distinct 模号 from @表) a
create table #a(id varchar(10),id2 varchar(10))
insert #a values('001','A001')
insert #a values('001','A002')
insert #a values('001','A003')
insert #a values('002','A001')
insert #a values('002','D001')
insert #a values('002','F001')
select * from #a
select id,(select top 1 id2 from #a aa where aa.id=id order by newid()) as id2
from #a group by id