select 模号,max((select top 1 货品编号 from A aa where aa.模号=模号 order by newid())) from A group by 模号
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
select 模号,货品编号=max(货品编号) from 表--或: select 模号,货品编号=min(货品编号) from 表--或:随机取一个: select 模号,货品编号=(select top 1 货品编号 from 表 where 模号=a.模号 order by newid()) from(select distinct 模号 from 表) a
--上面犯了一个错误,改一下: --取最小一个 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
--下面是测试--测试数据 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/*--测试结果--取最小一个模号 货品编号 ---------- ---------- 001 A003 002 F001(所影响的行数为 2 行) --取最大一个模号 货品编号 ---------- ---------- 001 A001 002 A001(所影响的行数为 2 行) --取第一个模号 货品编号 ---------- ---------- 001 A001 002 A001(所影响的行数为 2 行) --随机取一个模号 货品编号 ---------- ---------- 001 A002 002 F001(所影响的行数为 2 行) --*/
from A group by 模号
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
select 模号,货品编号=min(货品编号) from 表--或:随机取一个:
select 模号,货品编号=(select top 1 货品编号 from 表 where 模号=a.模号 order by newid()) from(select distinct 模号 from 表) a
--取最小一个
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
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/*--测试结果--取最小一个模号 货品编号
---------- ----------
001 A003
002 F001(所影响的行数为 2 行)
--取最大一个模号 货品编号
---------- ----------
001 A001
002 A001(所影响的行数为 2 行)
--取第一个模号 货品编号
---------- ----------
001 A001
002 A001(所影响的行数为 2 行)
--随机取一个模号 货品编号
---------- ----------
001 A002
002 F001(所影响的行数为 2 行)
--*/
from A
group by 模号