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?
-
2That 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)?Encomium– Encomium2021年12月15日 16:01:08 +00:00Commented 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.)GoSpazieren– GoSpazieren2021年12月20日 20:51:14 +00:00Commented 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.Encomium– Encomium2021年12月20日 21:09:41 +00:00Commented Dec 20, 2021 at 21:09
1 Answer 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'
-
1As 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.2021年12月15日 16:33:15 +00:00Commented 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.GoSpazieren– GoSpazieren2021年12月20日 15:37:40 +00:00Commented 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.tookiebunten– tookiebunten2021年12月21日 15:06:31 +00:00Commented Dec 21, 2021 at 15:06