我有一张这样的表
我尝试使用下面的代码来获取#34; AdID"小组
;with cte as
(
select
ADID,
PhotoName,
PhotoPath,
rank() over(partition by AdID order by PhotoName) as rn
from AdPhoto
)
select ADID,
PhotoName,
PhotoPath
from cte
where rn = 1
但结果是这样的输出
AD1001 temptemp_1.jpeg ~/UserUploads/temptemp/
AD1002 temptemp_10.jpg ~/UserUploads/temptemp/
我想要这样的输出
AD1001 temptemp_1.jpeg ~/UserUploads/temptemp/
AD1002 temptemp_5.jpg ~/UserUploads/temptemp/
请帮忙
答案 0 :(得分:2)
请尝试使用order by ID
代替order by PhotoName
:
;with cte as
(
select
ADID,
PhotoName,
PhotoPath,
rank() over(partition by AdID order by ID) as rn
from AdPhoto
)
select ADID,
PhotoName,
PhotoPath
from cte
where rn = 1
答案 1 :(得分:0)
这将有助于这组特定的数据:
;with cte as
(
select
ADID,
PhotoName,
PhotoPath,
rank() over(partition by AdID order by len(PhotoName) asc, PhotoName) as rn
from AdPhoto
)
select ADID,
PhotoName,
PhotoPath
from cte
where rn = 1
但是我建议要么改变光子命名的命名方案,要么找一些其他的排序算法。