34,593
社区成员
发帖
与我相关
我的任务
分享
select PCcode,PCFuName,PCShName,PCWholeName,
PCType from v_bk_PortCity order by PCWholeName
结果如下:
PCcode PCFuName PCShName PCWholeName PCType
368 AAL 奥勒松 Aalesund P
651 AAL 奥勒松 Aalesund C
322 AAR 奥胡斯 Aarhus P
606 AAR 奥胡斯 Aarhus C
232 ABR 阿伯丁 Aberdeen C
256 ABR 阿伯丁 Aberdeen P
987 AED 阿伯丁 Aberdeen C
241 ABI Abilene Abilene C
431 AUH 阿布扎比 Abu Dhabi P
1294 AUH 阿布扎比 Abu Dhabi C
想要的结果:
PCcode PCFuName PCShName PCWholeName PCType
368 AAL 奥勒松 Aalesund P
322 AAR 奥胡斯 Aarhus P
232 ABR 阿伯丁 Aberdeen C
987 AED 阿伯丁 Aberdeen C
241 ABI Abilene Abilene C
431 AUH 阿布扎比 Abu Dhabi P
就是只要PCFuName PCShName 相同的只取一条记录
select * from v_bk_portCity where pccode=(select max(pccode) from v_bk_portCity A where a.pcfuname =v_bk_portCity.pcfuname and a.pcshname=v_bk_portCity.pcshname)
set nocount on ;
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([PCcode] int,[PCFuName] nvarchar(3),[PCShName] nvarchar(7),[PCWholeName] nvarchar(9),[PCType] nvarchar(1))
Insert #T
select 368,N'AAL',N'奥勒松',N'Aalesund',N'P' union all
select 651,N'AAL',N'奥勒松',N'Aalesund',N'C' union all
select 322,N'AAR',N'奥胡斯',N'Aarhus',N'P' union all
select 606,N'AAR',N'奥胡斯',N'Aarhus',N'C' union all
select 232,N'ABR',N'阿伯丁',N'Aberdeen',N'C' union all
select 256,N'ABR',N'阿伯丁',N'Aberdeen',N'P' union all
select 987,N'AED',N'阿伯丁',N'Aberdeen',N'C' union all
select 241,N'ABI',N'Abilene',N'Abilene',N'C' union all
select 431,N'AUH',N'阿布扎比',N'Abu Dhabi',N'P' union all
select 1294,N'AUH',N'阿布扎比',N'Abu Dhabi',N'C'
Go
select [PCcode],[PCFuName],[PCShName],[PCWholeName],[PCType]
from
(Select *,row=row_number()over(partition by [PCFuName],[PCShName] order by [PCcode] ) from #T)T
where row=1
order by PCWholeName
PCcode PCFuName PCShName PCWholeName PCType
----------- -------- -------- ----------- ------
368 AAL 奥勒松 Aalesund P
322 AAR 奥胡斯 Aarhus P
232 ABR 阿伯丁 Aberdeen C
987 AED 阿伯丁 Aberdeen C
241 ABI Abilene Abilene C
431 AUH 阿布扎比 Abu Dhabi P
select * from tb1 where pccode in(select min(pccode) from tb1 group by PCFuName,PCShName)
select * from v_bk_PortCity a
where not exists (
select 1 from v_bk_PortCity
where PCFuName=a.PCFuName and PCShName=a.PCShName
and PCcode<a.PCcode
)