2017-06-12 4 views
0

ich diese beiden Anfragen bekam, aber kann nicht meinen Kopf um, wie sie zu kombinieren:Laufende Unterabfrage für jede Zeile

SELECT a.trainid, a.time AS departuretime, b.time AS arrivaltime, (b.timestamp-a.timestamp) AS journeytime, a.estimate as estimateA, b.estimate as estimateB 
FROM public.snapshot AS a 
INNER JOIN public.snapshot AS b ON a.trainid=b.trainid 
WHERE a.location = '49K' AND NOT a.location=b.location AND (b.timestamp-a.timestamp) BETWEEN 10 AND 5000 

und

SELECT a.location, ROUND(AVG(b.timestamp-a.timestamp)) AS avgjourneytime, ROUND(AVG(a.estimate)) as avgestimateA, ROUND(AVG(b.estimate)) as avgestimateB, COUNT(*) as records 
FROM public.snapshot AS a INNER JOIN public.snapshot AS b ON a.trainid=b.trainid 
WHERE (b.timestamp-a.timestamp) BETWEEN 180 AND 5000 AND ((#a.timestamp from query above#)%86400-a.timestamp%86400) BETWEEN -900 AND 900 AND a.location = '49K' 
GROUP BY a.location 

Also im Grunde möchte ich als Ausgabe

a.trainid, departuretime, arrivaltime, journeytime, estimateA, estimateB, avgjourneytime, avgestimateA, avgestimateB, records 

Beispieldaten:

a.trainid, departuretime, arrivaltime, journeytime, estimateA, estimateB, avgjourneytime, avgestimateA, avgestimateB, records 
54, 14:00, 14:05, 300, 600, 400, 320, 620, 420, 3 
57, 15:00, 15:10, 600, 800, 400, 860, 420, 420, 4 

Also für alle Durchschnittswerte, ich will nur die Datensätze, die waren an einem vorherigen Tag +/- 15 Minuten der Abfahrtszeit dieser Zeile, so dass ich für die erste Zeile nur den Durchschnitt der Züge, die abgereist sind zwischen 13:45 und 14:15, und für die zweite Reihe der Durchschnitt aller Züge zwischen 14:45 und 15:15. Datensätze gibt an, wie viele Datensätze für den Durchschnitt berücksichtigt wurden. Gibt es auch eine Möglichkeit, zwischen Wochentagen, Samstagen und Sonntagen zu unterscheiden?

Die Durchschnittswerte liegen innerhalb von +/- 15 Minuten an einem vorherigen Tag, die Zeitstempelwerte sind Sekunden seit 01.01.1970 - UTC, also ist Modulo 86400 ein Tag.

Verwenden von Postgres.

+2

uns zeigen, was Sie bisher versucht haben. Alles, was Sie bisher getan haben, ist, dass Sie uns bitten, etwas für Sie zu schreiben – Takarii

Antwort

0

die Sie interessieren ..

select q1.*,q2.* from 
(
    SELECT a.trainid as trainid, a.time AS departuretime, b.time AS arrivaltime, (b.timestamp-a.timestamp) AS journeytime, a.estimate as estimateA, b.estimate as estimateB 
    FROM public.snapshot AS a 
    INNER JOIN public.snapshot AS b ON a.trainid=b.trainid 
    WHERE a.location = '49K' AND NOT a.location=b.location AND (b.timestamp-a.timestamp) BETWEEN 10 AND 5000 
)q1, 
(
    SELECT a.trainid as trainid,a.location, ROUND(AVG(b.timestamp-a.timestamp)) AS avgjourneytime, ROUND(AVG(a.estimate)) as avgestimateA, ROUND(AVG(b.estimate)) as avgestimateB, COUNT(*) as records 
    FROM public.snapshot AS a INNER JOIN public.snapshot AS b ON a.trainid=b.trainid 
    WHERE (b.timestamp-a.timestamp) BETWEEN 180 AND 5000 AND ((#a.timestamp from query above#)%86400-a.timestamp%86400) BETWEEN -900 AND 900 AND a.location = '49K' 
    GROUP BY a.location 
)q2 
where q1.trainid=q2.trainid;