[Qgis-user] Functional dependency seem not working in group

Bo Victor Thomsen bo.victor.thomsen at gmail.com
Tue Oct 18 03:47:37 PDT 2022


Hi Peter -

(This comment has nothing to do with your original question; however, 
the query caught my eye):

You query is probably not as efficient as it can be. If you read the 
documentation for PostGIS about "ST_Expand":

-- quote --

Pre version 1.3, ST_Expand was used in conjunction withST_Distance 
<https://postgis.net/docs/ST_Distance.html>to do indexable distance 
queries. For example,|geom && ST_Expand('POINT(10 20)', 10) AND 
ST_Distance(geom, 'POINT(10 20)') < 10|. This has been replaced by the 
simpler and more efficientST_DWithin 
<https://postgis.net/docs/ST_DWithin.html>function.

-- quote end --

So if you're using a PostGIS later than 1.3 you could improve the query 
by doing this:

drop table if exists stevns_test_urban_ped; create table 
stevns_test_urban_ped as select urban.id as id, urban.geom as geom, 
min(urban.pop2019) as pop2019, count(forest) as ped, 
min(urban.pop2019)/(count(forest)+1)::float as pop_forest_ped from 
(select * from stevns_test_p where lu_type = 1) as urban join (select * 
from stevns_test_p where lu_type = 2) as forest on 
*ST_DWithin(urban.geom, forest.geom, 1000.0)* group by urban.id*, 
urban.geom* ;

Med venlig hilsen / Best regards

Bo Victor Thomsen

Den 17-10-2022 kl. 09:04 skrev Hans Skov-Petersen via Qgis-user:
> Dear Michael
>
> Functional dependencies are, according the Net, applied in PostgreSQL:https://stackoverflow.com/questions/66065987/postgres-sql-column-must-appear-in-the-group-by-clause-or-be-used-in-an-aggrega
>
> And accordingly, you shouldn't worry about it in PostGIS. The funny thing is that sometime it works. Sometimes it doesn't.
>
> Cheers
> Hans
>
> -----Original Message-----
> From: Qgis-user<qgis-user-bounces at lists.osgeo.org>  On Behalf Of Reetz, Michael (NLPV) via Qgis-user
> Sent: 17. oktober 2022 08:47
> To:qgis-user at lists.osgeo.org
> Subject: Re: [Qgis-user] Functional dependency seem not working in group
>
> Hello Hans,
>
> as far as I know, it is an SQL rule, that all selected attributes that are not queried with an aggregate function have to be part of the group by clause. If you run that query, your database server will check the syntax and throw that error. I'm not sure if the SQL server realizes the 'functional dependency' in your query as assumed by you.
>
> Cheers,
> Michael
>
> -----Ursprüngliche Nachricht-----
> Von: Qgis-user<qgis-user-bounces at lists.osgeo.org>  Im Auftrag von Hans Skov-Petersen via Qgis-user
> Gesendet: Montag, 17. Oktober 2022 08:00
> An:Qgis-user at lists.osgeo.org
> Betreff: [Qgis-user] Functional dependency seem not working in group
>
>
> Dear Friends
>
> I have, what appears to be a classic problem: When running a selection including a group statement I get the good old 'ERROR:  column "urban.geom" must appear in the GROUP BY clause or be used in an aggregate function'.
>
> I thought 'functional dependency' was in place to take care of that. The way I understand it, is that functional dependency will, as applied in e.g. the group statement, realize that e.g. the geom field is uniquely related to the id field (which is referred in the group statement). Am I right in my assumption?
>
> This is the ode causing the trouble:
>
> drop table if exists stevns_test_urban_ped; create table stevns_test_urban_ped as
>    select urban.id as id, urban.geom as geom, min(urban.pop2019) as pop2019, count(forest) as ped, min(urban.pop2019)/(count(forest)+1)::float as pop_forest_ped
>    from (select * from stevns_test_p where lu_type = 1) as urban
>    join (select * from stevns_test_p where lu_type = 2) as forest on ST_Distance(urban.geom, forest.geom) < 1000 and forest.geom && (ST_Expand(urban.geom, 1000))
>    group by urban.id
> ;
>
> Never mind the logics or meaning :-)
>
> Thanks
> Cheers
> Hans
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info:https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-user&data=05%7C01%7Chsp%40ign.ku.dk%7C1eb7efbdf32e4c3e425908dab00c602f%7Ca3927f91cda14696af898c9f1ceffa91%7C0%7C0%7C638015864588678289%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=outXdPjG2JC0yvxdhzHRhHRtLv%2FOsq9%2B6%2FaxU4df1%2Bk%3D&reserved=0
> Unsubscribe:https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-user&data=05%7C01%7Chsp%40ign.ku.dk%7C1eb7efbdf32e4c3e425908dab00c602f%7Ca3927f91cda14696af898c9f1ceffa91%7C0%7C0%7C638015864588678289%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=outXdPjG2JC0yvxdhzHRhHRtLv%2FOsq9%2B6%2FaxU4df1%2Bk%3D&reserved=0
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info:https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-user&data=05%7C01%7Chsp%40ign.ku.dk%7C1eb7efbdf32e4c3e425908dab00c602f%7Ca3927f91cda14696af898c9f1ceffa91%7C0%7C0%7C638015864588678289%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=outXdPjG2JC0yvxdhzHRhHRtLv%2FOsq9%2B6%2FaxU4df1%2Bk%3D&reserved=0
> Unsubscribe:https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fqgis-user&data=05%7C01%7Chsp%40ign.ku.dk%7C1eb7efbdf32e4c3e425908dab00c602f%7Ca3927f91cda14696af898c9f1ceffa91%7C0%7C0%7C638015864588678289%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=outXdPjG2JC0yvxdhzHRhHRtLv%2FOsq9%2B6%2FaxU4df1%2Bk%3D&reserved=0
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info:https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe:https://lists.osgeo.org/mailman/listinfo/qgis-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20221018/e82fa8f1/attachment-0001.htm>


More information about the Qgis-user mailing list