Thread: Join query query
Hi, I'd like to do something which I think should be quite easy - that is join 2 tables and create a new table. Table A postcode_input has columns which include postcode, eastings, northings. there are 1,687,605 rows. Table B bng_lat_long has columns lat, lon, e, n. There are 1,687,605 rows. eastings = e and northings = n so there should be a 1 to 1 match. The eastings northings pair should be unique in aggregate. So I tried doing this: SELECT A.postcode, A.eastings, A.northings, B.lat, B.lon INTO postcode_lat_long FROM postcode_input AS A LEFT JOIN bng_lat_long AS B On A.eastings = B.e AND A.northings = B.n And ended up with a table 13,708,233 rows long with what looks like plenty of duplicated rows. Some but not all are duplicated. What can I do to sort this out? Thanks, Andy
On Wed, Feb 13, 2013 at 3:01 PM, Andrew Taylor <andydtaylor@gmail.com> wrote: > And ended up with a table 13,708,233 rows long with what looks like plenty > of duplicated rows. Some but not all are duplicated. What can I do to sort > this out? It means that (e, n) pairs are not unique in A and B and you got a superposition of them. If you have 5 equal pairs in A and 7 same pairs with in B you will get 35 combinations as a result. And BTW when you use LEFT JOIN if there are rows in A that have no matching pairs in B you will get one row for each of them where lan and lon are NULLs. See the join_type section here http://www.postgresql.org/docs/9.2/static/sql-select.html. -- Sergey Konoplev Database and Software Architect http://www.linkedin.com/in/grayhemp Phones: USA +1 415 867 9984 Russia, Moscow +7 901 903 0499 Russia, Krasnodar +7 988 888 1979 Skype: gray-hemp Jabber: gray.ru@gmail.com
On Thursday, February 14, 2013 4:31 AM Andrew Taylor wrote: > Hi, > I'd like to do something which I think should be quite easy - that is = join 2 tables and create a new table. > Table A postcode_input has columns which include postcode, eastings, northings. there are 1,687,605 rows. > Table B bng_lat_long has columns lat, lon, e, n. There are 1,687,605 = rows. > eastings =3D e and northings =3D n so there should be a 1 to 1 match. = The eastings northings pair should be unique in=20 > aggregate. I think mapping is m to n. >So I tried doing this: > SELECT A.postcode, A.eastings, A.northings, B.lat, B.lon INTO postcode_lat_long > FROM postcode_input AS A > LEFT JOIN bng_lat_long AS B On A.eastings =3D B.e AND A.northings =3D = B.n > And ended up with a table 13,708,233 rows long with what looks like = plenty of duplicated rows. Some but not all are=20 > duplicated. What can I do to sort this out?=A0 What is you exact expection of data in postcode_lat_long? From the above it seems you want distinct rows which match between postcode_input and bng_lat_long, if there is no match then take the = values of postcode_input and NULL for bng_lat_long. If I am right, then you can try with using DISTINCT operator: http://www.postgresql.org/docs/9.2/static/sql-select.html#SQL-DISTINCT With Regards, Amit Kapila.