You could use the Distance to nearest hub algorithm from the Processing plugin which you could add your points and your lines layer instead of going through a query.
![Distance to nearest hub](https://i.stack.imgur.com/pVLfE.png)
From a couple of example layers that I have, you can get something like this:
![Output player for Distance to nearest hub analysis](https://i.stack.imgur.com/ayn6z.png)
Then when you open up the Attributes Table for the output layer, you will be given the HubName and HubDist in whatever measurement scale you selected (in my case, meters). Hope this helps.
If I understand correct, your question consists of 2 parts:
1. How to get many linestring to merge to 1
2. How to get the point 1000 km further down the road from point 1
Part 1 needs some more explanation from you. It seems you already did a linemerge and that works for me as well with the data you give. Maybe you can post a new stackexchange question with what precisely is not working for you. Merging linestrings from a line spaghetti can be difficult!
Part 2 I'll answer here.
Your query was already well on the way but needed some cleanup and the use of the 'measurement' option in postgis. One more important part of it is that you first have to reproject your dataset to something else than lat/lon, because you would get distance in degrees otherwise. Here is what I made of it:
WITH lines AS (
--CREATE THE INITIAL DATASET WITH LINESTRINGS
SELECT ST_LineFromText('LINESTRING(-1.3326397 50.9174932,-1.3319842 50.9166939)',4326) geom
UNION ALL
SELECT ST_LineFromText('LINESTRING(-1.3333297 50.9183351,-1.3332187 50.9182141,-1.3330436 50.9179954,-1.3326397 50.9174932)',4326) geom
UNION ALL
SELECT ST_LineFromText('LINESTRING(-1.3338982 50.9186728,-1.333581 50.9185143,-1.3333297 50.9183351)',4326) geom
UNION ALL
SELECT ST_LineFromText('LINESTRING(-1.3341242 50.9187719,-1.3338982 50.9186728)',4326) geom
UNION ALL
SELECT ST_LineFromText('LINESTRING(-1.335291 50.919197,-1.3346072 50.918916,-1.3341242 50.9187719)',4326) geom
)
,route AS (
--MERGE THEM INTO ONE LINE AND TRANSFROM TO MERCATOR PROJECTION
SELECT ST_Transform(ST_MakeLine(geom),900913) geom FROM lines
)
,routem AS (
--ADD A MEASUREMENT ALONG THE LINE FROM 0 TO THE TOTAL LENGTH AND STORE THE LENGTH (for later use)
SELECT ST_AddMeasure(route.geom, 0, ST_Length(route.geom)) geom, ST_Length(route.geom) l FROM route
)
,point AS (
--CREATE OUR INITIAL POINT AND TRANSFORM TO MERCATOR PROJECTION
SELECT ST_Transform(ST_PointFromText('POINT(-1.3333297 50.9183351)',4326),900913) geom
)
SELECT
ST_AsText(point.geom) as startpoint,
ST_AsText(
--GET THE SECOND POINT ACCORDING TO DISTANCE ALONG LINE
ST_LocateAlong(routem.geom,
--GET THE DISTANCE OF THE FIRST POINT AND ADD SOME DISTANCE (here 100m)
ST_LineLocatePoint(routem.geom, point.geom) * l + 100)
) endpoint
FROM
routem,
point
I'm sure there is more than 1 way to get this done, but I believe this is the easiest and shortest. The linear referencing tools in postgis can be a little bit confusing, since some of them work with fractions and others with real measurements. Reading the docs a couple of times helped for me ;)
Hope it serves you.
Best, Tom
Best Answer
Take a look at ST_ShortestLine. http://postgis.net/docs/manual-2.1/ST_ShortestLine.html
ST_ClosestPoint is the first point in ST_ShortestLine.
Something like this should work.
SELECT DISTINCT ON (a.id) ST_ShortestLine(a.geom,b.geom) FROM table1 a,table2 b ORDER BY a.id,ST_Distance(a.geom,b.geom);