RT:大家好!数据如:ID store_id store_nm1 storenm2
1 1 北京 上海
2 1 北京 南京
3 1 北京 武汉
4 2 上海 南京
5 3 广州 北京
6 3 广州 上海
... ... ... ...下sql语句后,要的效果:
1 1 北京 上海
2 2 上海 南京
3 3 广州 北京
... ... ... ...store_nm1相同的,只取其中一条,并且store_nm2在多条中取最小的。谢谢!
1 1 北京 上海
2 1 北京 南京
3 1 北京 武汉
4 2 上海 南京
5 3 广州 北京
6 3 广州 上海
... ... ... ...下sql语句后,要的效果:
1 1 北京 上海
2 2 上海 南京
3 3 广州 北京
... ... ... ...store_nm1相同的,只取其中一条,并且store_nm2在多条中取最小的。谢谢!
where not exists(select 1 from tb where store_nm1=a.store_nm1 and store_nm2<a.store_nm2)
into #testtb
from tablename a , tablename b
where a.storenm2 = b.store_nm1
go
select id, store_idnm1, store_nm1, storenm2 into #testta
from #testtb c
where not exists(select 1 from #testtb where c.store_idnm1=store_idnm1 and a.store_idnm2>store_idnm2)
go
select ROW_NUMBER() over(order by id) as id , store_idnm1, store_nm1, storenm2
from #testtb
select * from TA where id in
(select id from
(select min(id),store_id from ta
group by store_id)
)