Wait - I was just working away in QGIS version 1.7.3 and my plugin code like below works, but not in my dev version (1.9.90):<br><font face="'PrimaSans BT,Verdana,sans-serif'"><pre class="lang-py prettyprint"><code><span class="typ"><br>Query</span><span class="pun">=</span><span class="str">"select param_id, st_union(the_geom) as the_geom from myTable group by param_id"</span><span class="pln"><br>uri</span><span class="pun">.</span><span class="pln">setDataSource</span><span class="pun">(</span><span class="str">''</span><span class="pun">,</span><span class="pln"> </span><span class="str">'(%s)'</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> postGisQuery</span><span class="pun">,</span><span class="pln"> </span><span class="str">"the_geom"</span><span class="pun">,</span><span class="str">''</span><span class="pun">,</span><span class="str">'param_id'</span><span class="pun">)</span></code><code><span class="pln"><br>vlayer1 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QgsVectorLayer</span><span class="pun">(</span><span class="pln">uri</span><span class="pun">.</span><span class="pln">uri</span><span class="pun">(),</span><span class="pln"> </span><span class="str">'testlayer1'</span><span class="pun">,</span><span class="pln"> </span><span class="str">"postgres"</span><span class="pun">)</span><span class="pln"> </span></code><br></pre></font>So it allowed the use of param_id as a key, even though it isn't a primary key. Has something changed in recent versions that doesn't allow such code? In fact there doesn't seem to be an error message in the Log Messages, QgsVectorLayer just doesn't load the layer - using QgsMapLayerRegistry.instance().addMapLayer(vlayer1)<br><br>Maybe I should check out the most recent version - or build 1.8 to check?<br><br>Ian<br><br>----- Original Message -----<br>From: Bernhard Ströbl &lt;bernhard.stroebl@jena.de&gt;<br>Date: Thursday, May 10, 2012 10:04 am<br>Subject: Re: [Qgis-developer] Using group by to set a PostGIS datasource using QGIS Python API<br>To: Ian Packham &lt;i.packham@ucd.ie&gt;<br>Cc: qgis-developer@lists.osgeo.org<br><br>&gt; Hi Ian,<br>&gt; <br>&gt; I am always using views for such things. I am not sure (never <br>&gt; tried) if you can use a subselect as table parameter for <br>&gt; uri.setDataSource.So I would create a view on the DB. Be sure, <br>&gt; that the id field of the view is defined as the pk in its source <br>&gt; table (no combined pk) and that the values in the id field are unique.<br>&gt; <br>&gt; Bernhard<br>&gt; <br>&gt; Am 10.05.2012 10:56, schrieb Ian Packham:<br>&gt; &gt;Hi<br>&gt; &gt;<br>&gt; &gt;I asked the following question at gis.stackexchange.com, but <br>&gt; had no luck<br>&gt; &gt;so far. Can any of you guys help?<br>&gt; &gt;<br>&gt; &gt;Ian<br>&gt; &gt;<br>&gt; &gt;http://gis.stackexchange.com/questions/24941/using-group-by-to-<br>&gt; set-a-postgis-datasource-using-qgis-python-api<br>&gt; &gt;<br>&gt; &gt;I am writing a QGIS Plugin and wish to obtain data from a <br>&gt; PostGIS table<br>&gt; &gt;using the Group By clause using something like the following code:<br>&gt; &gt;<br>&gt; &gt;|uri=&nbsp; QgsDataSourceURI()<br>&gt; &gt;uri.setConnection('localhost',&nbsp; '5432',&nbsp; <br>&gt; 'postgis',&nbsp; 'postgres',&nbsp; 'xxx')<br>&gt; &gt;Query="select param_id, st_union(the_geom) as the_geom from <br>&gt; myTable group by param_id"<br>&gt; &gt;uri.setDataSource('',&nbsp; '(%s)'&nbsp; %&nbsp; <br>&gt; postGisQuery,&nbsp; "the_geom",'','gid')<br>&gt; &gt;vlayer1=&nbsp; QgsVectorLayer(uri.uri(),&nbsp; <br>&gt; 'testlayer1',&nbsp; "postgres")<br>&gt; &gt;|<br>&gt; &gt;<br>&gt; &gt;This gives the error "No key field for query given." because it is<br>&gt; &gt;looking for the gid key field. But if gid was put in the select<br>&gt; &gt;statement of the Query, we won't get aggregated rows, just distinct<br>&gt; &gt;rows. If I try an aggregation function on gid, I get the same <br>&gt; error. If<br>&gt; &gt;I try aliasing the aggregate, I get an "Erroneous query" error (even<br>&gt; &gt;|count(gid) as gid|), same as if I put 'param_id' as the key <br>&gt; field in<br>&gt; &gt;setDataSource, because 'param_id' is not a primary key, I guess.<br>&gt; &gt;<br>&gt; &gt;In this case, forcing the key field in the uri seems to make <br>&gt; the group<br>&gt; &gt;by clause useless. Is there another workaround I haven't <br>&gt; thought of or<br>&gt; &gt;is this the expected behaviour?<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;_______________________________________________<br>&gt; &gt;Qgis-developer mailing list<br>&gt; &gt;Qgis-developer@lists.osgeo.org<br>&gt; &gt;http://lists.osgeo.org/mailman/listinfo/qgis-developer<br>&gt; &gt;<br>