日期:2014-05-18 浏览次数:20672 次
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([id] int,[ch] varchar(7))
insert [test]
select 1,'wwk-222' union all
select 2,'wwk-222' union all
select 3,'wwk-086' union all
select 4,'wwk-076' union all
select 5,'wwk-089' union all
select 6,'wwk-079'
select COUNT(distinct [ch]) as [ch] from test
/*
ch
------
5
*/
--去掉重复使用distinct
------解决方案--------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([id] int,[ch] varchar(7))
insert [test]
select 1,'wwk-222' union all
select 2,'wwk-222' union all
select 3,'wwk-086' union all
select 4,'wwk-076' union all
select 5,'wwk-089' union all
select 6,'wwk-079'
select * from test a
where a.id=(select MIN(b.id) from test b where a.ch=b.ch)
order by id
--相同的ch取最大id的时候把min改成max
/*
id ch
-------------------
1 wwk-222
3 wwk-086
4 wwk-076
5 wwk-089
6 wwk-079
*/