问题
I have table in PostgreSQL database.
The table below shows you the hourly speed of trains in each underground station of the cities of England:
DATE_KEY | STATION | CITY | SPEED
-------------------------------------------------------
2018-10-01 00:00:00 | Arsenal | London | 1078.125
2018-10-01 01:00:00 | Arsenal | London | 877.222
2018-10-01 02:00:00 | Arsenal | London | 1127.752
2018-10-01 00:00:00 | Beckton | London | 2866.375
2018-10-01 01:00:00 | Beckton | London | 1524.375
2018-10-01 02:00:00 | Beckton | London | 1618.533
2018-10-01 00:00:00 | Chesham | Liverpool | 1567.588
2018-10-01 01:00:00 | Chesham | Liverpool | 792.333
2018-10-01 02:00:00 | Chesham | Liverpool | 1138.857
2018-10-01 00:00:00 | Farringdon | Liverpool | 1543.625
2018-10-01 01:00:00 | Farringdon | Liverpool | 538.666
2018-10-01 02:00:00 | Farringdon | Liverpool | 1587.583
I'm trying to get aggregated data like this:
DATE_KEY | CITY | AVG_SPEED
----------------------------------------------------
2018-10-01 00:00:00 | London | 852.125
2018-10-01 01:00:00 | London | 750.222
2018-10-01 02:00:00 | London | 625.752
2018-10-01 00:00:00 | Liverpool | 804.588
2018-10-01 01:00:00 | Liverpool | 792.333
2018-10-01 02:00:00 | Liverpool | 952.857
In other words, I need in result the hourly average (AVG) of trains speed in the city.
回答1:
I think all you need is to use AVG function with group by clause like:
SELECT DATE_KEY, CITY, AVG(SPEED) as AVG_SPEED
FROM table
GROUP BY DATE_KEY, CITY
回答2:
I know the dataset mentioned in the question is hourly values, if you want to compute the average across different timestamps (like "2018-10-01 02:45:08") then you can average speeds for each hour like this -
select DATE_TRUNC('day', a.DATE_KEY)+cast(DATE_PART('hour',a.DATE_KEY) as Integer)/1*INTERVAL '1 hour' as hour_key, city, avg(speed) as avg_speed
FROM table1 a group by 1, 2;
来源:https://stackoverflow.com/questions/52990917/how-correctly-aggregate-date-in-postgresql-database