1

我有一个包含电话号码的应用程序表。我正在尝试选择按 ApplicationId 降序排序的前 10000 行(以获取最新的应用程序),但我想删除所有重复的电话号码。

我试过了..

select distinct * from
(select top 10000 Forenames, PhoneNumber
from Application
order by ApplicationId desc) AS Applications

尽管此查询按 Forename 升序对其进行排序,即不再按 ApplicationId 顺序。

4

2 回答 2

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;
于 2013-07-26T11:03:03.530 回答
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
于 2013-07-26T11:06:16.990 回答