抓取除最小值之外的所有ID

时间:2017-07-28 17:23:11

标签: sql

我正在尝试从KeyLocationIDs获取其他NumberOfDuplicates。我想要排除Min(KeyLocationID)并想要其他人的列表。

select CompanyID, Location, EventCode, count(*) as NumberOfDuplicates, min(KeyLocationID) as KeepThisOne
from trip.KeyLocations
group by CompanyID, Location, EventCode
having count (*) > 1
order by location asc

2 个答案:

答案 0 :(得分:0)

你在这里缺少的是法令。 有几种方法可以做到这一点,这里有一些例子: 由@nobody吸食 或者你可以这样做:

select CompanyID, Location, EventCode, count(*) as NumberOfDuplicates, 
min(KeyLocationID) as KeepThisOne
from trip.KeyLocations
group by CompanyID, Location, EventCode
having KeyLocationID > min(KeyLocationID)
order by location asc

或者你可以这样做:

select CompanyID, Location, EventCode, count(*) as NumberOfDuplicates, 
min(KeyLocationID) as KeepThisOne
from trip.KeyLocations
group by CompanyID, Location, EventCode
having KeyLocationID != MIN(KeyLocationID)
order by location asc

答案 1 :(得分:0)

选择CompanyID,Location,EventCode,count()作为NumberOfDuplicates 来自trip.KeyLocations 哪里 KeyLocationID不在(通过CompanyID,Location,EventCode从trip.KeyLocations组中选择min(KeyLocationID)) 按CompanyID,Location,EventCode分组 有计数()> 1 按地点排序asc