我有一个包含电话号码的Application表。我正在尝试选择由ApplicationId降序排序的前10000行(以获取最新的应用程序),但我想删除所有重复的电话号码。
我试过了..
select distinct * from
(select top 10000 Forenames, PhoneNumber
from Application
order by ApplicationId desc) AS Applications
虽然此查询按Forename升序对其进行排序,即不再按ApplicationId顺序排序。
答案 0 :(得分:3)
我认为您想要的逻辑是:获取每个电话号码的最新应用ID,然后返回有关这些电话号码的信息。
select a.*
from (select limit 10000 PhoneNumber, max(ApplicationId) as maxaid
from Application a
group by PhoneNumber
order by max(aid) desc
) list join
Application a
on a.ApplicationId = list.maxaid
order by ApplicationId desc;
答案 1 :(得分:0)
SELECT * FROM (select top 10000 Forenames, PhoneNumber,
ROW_NUMBER() OVER(PARTITION BY PhoneNumber ORDER BY Forenames DESC) AS Row
from Application
order by ApplicationId desc) as temp
Where Row = 1