2017-07-28 2 views
0

Ich versuche, die anderen KeyLocationIDs von der NumberOfDuplicates zu bekommen. Ich möchte die Min(KeyLocationID) ausschließen und möchte eine Liste der anderen.Schnappen Sie sich alle IDs mit Ausnahme von Min Wert

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 
+1

WHERE KeyLocationID> min (KeyLocationID) '' vor GROUP BY' –

Antwort

0

Was Sie hier vermissen, ist die Wo-Bestimmung. Es gibt mehrere Möglichkeiten, dies zu tun, sind hier einige Beispiele: als von @nobody sugested oder Sie tun können:

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 

oder könnten Sie tun:

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

Leider diese beiden Rückkehr enthalten: Ein Aggregat wird möglicherweise nicht in der WHERE-Klausel, es sei denn, sie befindet sich in einer Unterabfrage in einer HAVING-Klausel oder einer Auswahlliste, und die aggregierte Spalte ist eine äußere Referenz. –

+0

Sie haben Recht, tut mir leid. Sie sollten diese auf die haben ... –

+0

Haben Sie ein Beispiel, ich weiß nicht, was Sie meinen –

0

wählen CompanyID, Standort, Eventcode, count () als NumberOfDuplicates von trip.KeyLocations wo KeyLocationID nicht in (select min (KeyLocationID) aus trip.KeyLocations Gruppe von CompanyID, Location, Eventcode) Gruppe von CompanyID, Location, Eventcode mit count ()> 1 , um nach Standort asc

Verwandte Themen