日期:2014-05-18 浏览次数:20426 次
--> 测试数据:[test] if object_id('[test]') is not null drop table [test] create table [test]( [ID] int, [Company] varchar(3), [Name] varchar(1), [Mobile] int ) insert [test] select 0,'IBM','R',123456 union all select 1,'KDO','P',987654 union all select 2,'JCN','S',234567 union all select 3,'KDO','K',345678 union all select 4,'IBM','L',654321 union all select 5,'HAL','W',765432 select * from test a where [ID]=(select MIN([ID]) from test b where a.Company=b.Company) order by ID /* ID Company Name Mobile 0 IBM R 123456 1 KDO P 987654 2 JCN S 234567 5 HAL W 765432 */
------解决方案--------------------
--> 测试数据:[test] if object_id('[test]') is not null drop table [test] create table [test]( [ID] int, [Company] varchar(3), [Name] varchar(1), [Mobile] int ) insert [test] select 0,'IBM','R',123456 union all select 1,'KDO','P',987654 union all select 2,'JCN','S',234567 union all select 3,'KDO','K',345678 union all select 4,'IBM','L',654321 union all select 5,'HAL','W',765432 --2005以上版本 with t as( select px=ROW_NUMBER()over(partition by Company order by id), * from test ) select ID,Company,Name,Mobile from t where px=1 order by id /* ID Company Name Mobile 0 IBM R 123456 1 KDO P 987654 2 JCN S 234567 5 HAL W 765432 */