简体   繁体   中英

How can I alter this to run in postgresql?

What needs changing for this to run in postgreSQL?

I was given the piece of sql

UPDATE ACC
SET ACC.ACC_EC = SITESmin.ACC_EC, 
    ACC.ACC_NC = SITESmin.ACC_NC
FROM    ACC
       INNER JOIN LATERAL ( SELECT TOP 1
                                *
                      FROM      SITES
                      ORDER BY  ( acc_ec - site_etg ) * ( acc_ec - site_etg ) + (acc_ncb - site_ntg ) * ( acc_ncb - site_ntg )
                ) SITESmin;

It seems to be using SET but I do not know why, so if it's not needed drop it. I am trying to get postgresql to work out distances. For every record in file one I have to compare to 3300 records in file 2 and select the nearest. Received wisdom suggests an array solution for the 3300 but I do not know how to do that. Perhaps it it a "sub query" in SQL.

If I am permitted to upload samples I will do so, though I have the feeling this is not allowed?

Here are the filed names

public.acc.Location_Easting_OSGR 
public.acc.Location_Northing_OSGR
"public"."Sites"."SITE_ETG" 
"public"."Sites"."SITE_NTG" 

Try this:

WITH SITESmin as (
    SELECT ACC_EC, ACC_NC
    FROM      SITES
    ORDER BY  ( acc_ec - site_etg ) * ( acc_ec - site_etg ) + (acc_ncb - site_ntg ) * ( acc_ncb - site_ntg )
    LIMIT 1
)
UPDATE ACC
SET ACC.ACC_EC = SITESmin.ACC_EC,
    ACC.ACC_NC = SITESmin.ACC_EC
FROM SITESmin;

If it does not work, please provide the schema and some data to make it easier to reproduce

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM