2016-06-03 14 views
0

Ich habe eine GIS-Schicht, die Tausende von Facility IDs hat.GIS SQL Query Logic Syntax

  • Für jede Einrichtungs-ID können mehrere chemische IDs vorhanden sein.
  • Für jede chemische ID gibt es maximal 3 Zonentypen. Zone A, B, C. Aber es gibt nicht immer 3 Zone Typen, es variiert.
  • Und die Anzahl der chemischen IDs für jede Facility ID kann von 1 bis zu vielen variieren.

Was ich zu tun versuchen, ist auflösen (oder eine Gruppe von) diese Schicht nach unten angezeigt werden, welche Einrichtung ID Zone die größte Pufferabstand hat, während darunter die ZoneType (wichtig)

Hier sind die Felder

   FacilityID, ChemicalID, ZoneType, ZoneDistance 
ex rows:  1    2   A   1000 
       1    2   B   900 
       1    2   C   500 
       1    5   A   1200 
       1    5   B   900 
       1    7   B   2000 
       1    7   C   900 
       2    13   A   200 
       2    13   B   300 
       2    13   C   600 

erwartetes Ergebnis: 1 Zeile für jede FacilityID mit dem Max Buffer und dem spezifischen Zonentyp. Also für FacilityID 1- mag ich eine Zeile, und es wäre ZoneType B mit einem ZoneDistance von 2000

FacilityID,ZoneType, ZoneDistance 
    1   B   2000 
    2   C   600 

habe ich versucht, ein paar SQL-Anweisung, die ich die Einrichtungs-ID bekam mit der Max-ZoneDistance für jeden ZoneType. Ich möchte nur den Max ZoneDistance für alle ZoneTypes bei einer FacilityID.

SELECT max(ZoneDistance), ZoneType, FacilityID 
FROM AllZones group by ZoneType, FacilityID; 

Ich habe auch versucht, eine Subquery aber auch nicht

Ich bin halb neu zu SQL funktionierte, und ich kann nicht die Logik scheinen, um herauszufinden, um meine Ergebnisse. Antworten in SQL oder Python willkommen

+1

welche gis-Funktion verwenden Sie? SQL-Server, Postgres, Oracle? –

+0

'Max (ZoneDistance)' Mittlerer Pufferbereich, der alle 'ZoneType' von jeder Einrichtung abdeckt? –

+0

Ich verwende Microsoft Access – ziggy

Antwort

0
Select f.FacilityID, 
    f.ZoneType, 
    m.MaxZoneDistance 
from AllZones f 
    join (
      SELECT max(ZoneDistance) MaxZoneDistance, 
       FacilityID 
      FROM AllZones 
      group by FacilityID 
     ) m 
      on f.facilityID=m.facilityID 
       and MaxZoneDistance=ZoneDistance 

OUTPUT

FacilityID ZoneType MaxZoneDistance 
1   B   2000 
2   C   600 
0

Verwenden Sie dieses Beispiel im Zugriff. Ich bereite eine Demo auf MySQL vor.

Achieving ROW_NUMBER/PARTITION BY in MS Access

SQL Fiddle Demo

SELECT * 
FROM (
     SELECT 
      t1.`ZoneType`, 
      t1.`FacilityID`, 
      t1.`ZoneDistance`, 
      COUNT(*) AS `rn` 
     FROM  Facility AS t1 
     INNER JOIN Facility AS t2 
       ON t1.`FacilityID` = t2.`FacilityID` 
       AND t1.`ZoneType`  = t2.`ZoneType` 
       AND t1.`ChemicalID` = t2.`ChemicalID` 
       AND t1.`ZoneDistance` <= t2.`ZoneDistance` 
     GROUP BY 
      t1.`ZoneType`, 
      t1.`FacilityID`, 
      t1.`ZoneDistance` 
     ORDER BY 
      t1.`ZoneType`, 
      t1.`FacilityID`, 
      t1.`ZoneDistance` DESC 
     ) T 
WHERE rn = 1  
ORDER BY `ZoneType`,`FacilityID` 

OUTPUT

| ZoneType | FacilityID | ZoneDistance | rn | 
|----------|------------|--------------|----| 
|  A |   1 |   1200 | 1 | 
|  A |   2 |   200 | 1 | 
|  B |   1 |   2000 | 1 | 
|  B |   2 |   300 | 1 | 
|  C |   1 |   900 | 1 | 
|  C |   2 |   600 | 1 | 

Nachdem ich Ihr Ergebnis sah fand eine andere Methode

Second DEMO

SELECT * 
FROM (
     SELECT 
      t1.`FacilityID`, 
      t1.`ChemicalID`, 
      t1.`ZoneType`, 
      t1.`ZoneDistance`, 
      (SELECT COUNT(*) 
       FROM Facility as t2 
       WHERE t1.`FacilityID` = t2.`FacilityID` 
       AND ((t1.`ZoneDistance` < t2.`ZoneDistance`) 
        OR (t1.`ZoneDistance` = t2.`ZoneDistance` and t1.`ZoneType` > t2.`ZoneType`) 
        OR (t1.`ZoneDistance` = t2.`ZoneDistance` and t1.`ZoneType` = t2.`ZoneType` and t1.`ChemicalID` > t2.`ChemicalID`) 
        ) 
      ) as rn 
     FROM  Facility AS t1 
     ORDER BY 
      t1.`FacilityID`,   
      t1.`ZoneDistance` DESC, 
      t1.`ZoneType`, 
      t1.`ChemicalID` 
    ) T 
WHERE rn = 0; 

OUTPUT

FacilityID ChemicalID ZoneType ZoneDistance rn 
2   13   C   600    0 
1   7   B   2000   0