3

I'm relatively new to (Q)GIS .

Is it possible to use a sub query in the SQL Query Composer for a WFS layer?

I'm working with a big dataset, too big to download. Through WFS I want to load the features to QGIS that overlap with other features, that meet a specific set of criteria.

I was thinking this should be possible using a sub query:

SELECT * 
FROM Dataset 
WHERE ST_Overlaps(geometry, 
 (SELECT geometry 
 FROM Dataset 
 WHERE Attribute LIKE 'Attribute value'))

However, when I use this, I get the following error: SQL query is invalid: Syntax error. SELECT is unexpected. What is the solution?

enter image description here

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Dec 15, 2021 at 15:58
3
  • 2
    That query will fail in any case as you're selecting an entire row in your subquery rather than just a geometry field. Did you try a CTE? Or how about SELECT * FROM 'table' a inner join 'table' b on ST_Overlaps(a.geometrie,b.geometrie)? Commented Dec 15, 2021 at 16:01
  • Thank you so much for your reply. Starting with a CTE gives a syntax error: "an identifier is unexpected. SELECT is expected instead." Maybe the QGIS SQL query Composer is just only fit for basic queries? (IRT inner join: Since I am trying to join within the same table, I would not know how to do that without a CTE.) Commented Dec 20, 2021 at 20:51
  • you can inner join a table to itself by aliasing it. See my comment - table1 a inner join table1 b. Commented Dec 20, 2021 at 21:09

1 Answer 1

1

Your sub select is returning more than one field.

For example a quick and dirty script I've written today for work. If you select * from in sub query the main query can't match one to many

select CLIENT_REFERENCE||'/'||WOR_SEQ_NO
, job_number
, header_comments 
from CBG_URS_JOB_HEADER_IFACE_V3 
where CLIENT_REFERENCE||'/'||WOR_SEQ_NO in (select ALTREFS.WOR_REF from ALTREFS);

Also you screen shot doesn't make much sense your selecting all twice from the table Enkelbestemming, once in the main query and then again in the sub query

Would it not be better being something like

select * from enkelstemming 
where st_overlaps = (select value from table) 
and fid = '7943483'
answered Dec 15, 2021 at 16:26
3
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Commented Dec 15, 2021 at 16:33
  • Thank you for your reply. I believe your suggestion is not what im looking for. Maybe I should start with something a bit less complicated: is it possible to write a query that returns all features that overlap with any feature in the same table? Please ignore the screenshot, I tried to create an example quickly but made one that makes no sense. Commented Dec 20, 2021 at 15:37
  • It should be but with out seeing the data or how the tables are structured it's hard to say. If both tables share a column with the same data like an address then you can join on that. Commented Dec 21, 2021 at 15:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.