Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am sorry if the question is elementary but I did not find any clear discussion or guide to understand how to run a query in postgresql. So that a pointer to another website would be also useful. Anyhow, I have two tables:

Table 1

Table 2

Table 2 has been obtained from table 1 and it includes an additional column: depart/arrive. This column indicates if the coordinate (or row) is the first coordinate of a path (D) or the last coordinate (A).

Using postgres, I would like to use as common field of the two tables "day", "time", "lat", "lon" in order to obtain a new table 3 that includes all the fields of table 1 plus the "depart/arrive" field of table 2.

Table 3

I thank you in advance.

share|improve this question
@lynxlynxlynx please post as answer. – Brad Nesom Jan 29 '13 at 19:46

Everyone learning SQL should know about the LEFT JOIN. It basically joins everything from the left table to the "joins" on the right side. Where there are no matches, the right data is null, but the left side still is queried. Wikipedia has a good article on Joins.

SELECT,, A.time,, A.lon, B.depart_arrive
FROM table_1 AS A
LEFT JOIN table_2 AS B ON = AND = AND A.time = B.time AND
                 = AND A.lon = B.lon
ORDER BY,, A.time,, A.lon, B.depart_arrive;

This isn't "table_3", but you can SELECT ... INTO table_3 FROM ..., or create a view, so the joined data is always up-to-date in something that looks like a table.

On a side note, rather than storing two columns for day and time, it is more powerful to use one column for timestamp (with time zone). Same for your location data, which could be stored as a PostgreSQL point type, or PostGIS geometry or geography types. These will add other GIS features, and can improver the searching performance with an optimized index.

share|improve this answer
Using your SELECT after CREATE TABLE table_3 AS ... should do the trick. – underdark Jan 29 '13 at 21:08

you have the right idea but would need another field to concat them all into.
or since you are in posgres perhaps a view with the fields concat.
once you have the field the join will be completed as lynxlynxlynx shows.


  1. create a composite pk and fk. It may work from what I read.
  2. use the join
share|improve this answer
why concat? simple joins should do. – underdark Jan 29 '13 at 19:54
@underdark, Are you suggesting to join against both ID fields or some other means of joining day, time, lat, lng without concat? Can we assume ID between two tables are the same? – artwork21 Jan 29 '13 at 20:07
IF the OP is a savvy as yourself they may find the ability to create composite primary keys. however the creation of composite foreign key would be the next thing on the agenda. I only learned about them minutes ago. and I have been around databases a while. My software stack and expertise would still lead me to create my own composite field using concat. - downvote really? :(… – Brad Nesom Jan 29 '13 at 20:20
I agree with @artwork the question as stated mentions joining more than one column. Does postgres support composite pK and fk? – Brad Nesom Jan 29 '13 at 20:24
@artwork21 Mike already posted an answer relying only on SQL without any functions like concat. Imho, concat might be more obvious for users with a background in using spreadsheets but I don't think it should be recommended in a database setting. – underdark Jan 29 '13 at 21:12

Thank you all. The query works and I could visualize the result in the data output windows. In order to obtain table_3 I used SELECT,, A.time,, A.lon, B.depart_arrive INTO table_3 ...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.