select distinct GS from tablename
解决方案 »
- Sql Server 6.0和7.0 有什么区别?
- 求助,sql建立临时表问题
- 求一sql从新自动编号语句用set来执行的
- 触发器影响插入吗?
- 求一条拆分1个字段成多个字段的SQL语句(要高性能)
- SQL语句,将数据类型 varchar 转换为 float 时出错。
- 求救。四表中取数值。按位置摆放数据。
- mysql limit 转为sqlserver select top 语句
- 关于触发器 在线求救
- 这个问题可以直接用视图吗?还是非得拥油表处理?
- 如何用FTP建立两个数据库间的同步复制
- [急]排序。列是Varchar,SQL SERVER的Order不区别大小写,我想区别大小写,按ASCII的顺序排,有办法吗?
where ID=(select top 1 ID
from 表
where GS=t.GS
order by ID)
group by GS
(
ID varchar(10),
GS varchar(20)
)
insert @tb
select 'in113', '南保' union
select 'in114', '南保' union
select 'in104', '南平中保' union
select 'in106', '南平中保' union
select 'in105', '邵武中保' union
select 'in103', '邵武中保' --查询
select * from @tb t
where ID=(select top 1 ID
from @tb
where GS=t.GS
order by ID)
--结果
/*
ID GS
---------- --------------------
in103 邵武中保
in104 南平中保
in113 南保(3 row(s) affected)
*/
楼主要的: in105 邵武中保,
不是: in103 邵武中保
只是分组求出了最小的
可怎么求出前三个呢/
我哦看不明白
楼主的我也不太明白
declare @tb table
(
ID varchar(10),
GS varchar(20)
)
insert @tb
select 'in113', '南保' union all
select 'in114', '南保' union all
select 'in104', '南平中保' union all
select 'in106', '南平中保' union all
select 'in105', '邵武中保' union all
select 'in103', '邵武中保'
select * from @tb
drop table #tempSELECT IDENTITY(int, 1,1) AS ID_Num ,* into #temp FROM @tb select * from #temp
--结果
select id,gs from #temp where id_num in( select min(id_num) from #temp group by gs)