Search code examples
sqlsqlitespatialite

Syntax error while using centroid function within the RotateCoords function in Spatialite Query


CREATE TABLE rotated_bus AS SELECT AO_id,
RotateCoords(ST_Centroid(Geometry) FROM Substation, 45.00) AS Geometry FROM Busbar;

I am trying to rotate a line geometry (Busbar) by passing the centroid of a polygon geometry (Substation) inside the RotateCoords Function. After running the above query, I am getting an error “near FROM syntax error”. What is wrong with my query?


Solution

  • You could join the two tables:

    CREATE TABLE rotated_bus AS
    SELECT Busbar.AO_id,
           RotateCoords(ST_Centroid(Substation.Geometry), 45) AS Geometry
    FROM Busbar
    JOIN Substation ON Busbar.AO_id = Substation.Substn_ID
    

    Alternatively, use a correlated subquery (subqueries must be complete queries inside an extra pair of parentheses):

    CREATE TABLE rotated_bus AS
    SELECT AO_id,
           (SELECT RotateCoords(ST_Centroid(Geometry), 45)
            FROM Substation
            WHERE Substn_ID = Busbar.AO_id
           ) AS Geometry
    FROM Busbar