select * from tb t where not exists (select 1 from tb where t.点名 = 点名 and t.点编号>点编号) --这样可以不
select * from tb t where 点编号=(select min(点编号) from tb where 点名=t.点名)
6、7楼,你们考虑,经纬度值为NULL优先不为NULL的情况了吗?
create table tb(点编号 int,点名 nvarchar(10),坐标系统 nvarchar(10),经纬度 nvarchar(10)) insert into tb select 1,'AA','北京54','123,23' insert into tb select 2,'AA','本地坐标',NULL insert into tb select 3,'BB','北京54','123,23.5' insert into tb select 4,'BB','本地坐标','123.5,23' insert into tb select 5,'CC','北京54',NULL insert into tb select 6,'CC','本地坐标',NULL go select * from tb where 点编号 in( select min(点编号) from tb where 经纬度 is not null group by 点名 --找出某点名中经纬度不为NULL时的最小点编号 union all select min(点编号) from tb a where not exists(select 1 from tb where 点名=a.点名 and 经纬度 is not null)--找出某点名中经纬度全为NULL的最小点编号 ) /* 点编号 点名 坐标系统 经纬度 ----------- ---------- ---------- ---------- 1 AA 北京54 123,23 3 BB 北京54 123,23.5 5 CC 北京54 NULL(3 行受影响)*/ go drop table tb
where not exists
(select 1 from tb where t.点名 = 点名 and t.点编号>点编号)
--这样可以不
insert into tb select 1,'AA','北京54','123,23'
insert into tb select 2,'AA','本地坐标',NULL
insert into tb select 3,'BB','北京54','123,23.5'
insert into tb select 4,'BB','本地坐标','123.5,23'
insert into tb select 5,'CC','北京54',NULL
insert into tb select 6,'CC','本地坐标',NULL
go
select * from tb where 点编号 in(
select min(点编号) from tb
where 经纬度 is not null group by 点名 --找出某点名中经纬度不为NULL时的最小点编号
union all
select min(点编号) from tb a
where not exists(select 1 from tb where 点名=a.点名 and 经纬度 is not null)--找出某点名中经纬度全为NULL的最小点编号
)
/*
点编号 点名 坐标系统 经纬度
----------- ---------- ---------- ----------
1 AA 北京54 123,23
3 BB 北京54 123,23.5
5 CC 北京54 NULL(3 行受影响)*/
go
drop table tb
min(点编号)是适用于数字吧,我的编号其实是用的字符串