I have a table in PostgreSQL that contains postcode data as below
| postcode | lat | long | district |
|---|---|---|---|
| AB1 0AA | 57.101474 | -2.242851 | Aberdeen City |
| AB1 0AB | 57.102554 | -2.246308 | Aberdeen City |
| AB1 0AD | 57.100556 | -2.248342 | Aberdeen City |
| AB1 0AR | 57.091357 | -2.224831 | Aberdeenshire |
| AB1 0AS | 57.083838 | -2.234437 | Aberdeenshire |
| AB1 0AT | 57.089299 | -2.239768 | Aberdeenshire |
I would like to find out the two postcodes by district that are furthest apart. I know I can use PostGIS similar to the following to calculate the distance between two sets of lat long:
st_distancesphere(st_makepoint(lat1, long1), st_makepoint(lat2, long2))
But how would I use a window function to do this across all combinations of postcodes within a district group?
I'm using PostgreSQL 12.7 on AWS RDS.