<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.E-postmall17
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="SV" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Thanks Felix!<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">Från:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> postgis-users [mailto:postgis-users-bounces@lists.osgeo.org]
<b>För </b>Felix Kunde<br>
<b>Skickat:</b> den 9 mars 2020 10:33<br>
<b>Till:</b> postgis-users@lists.osgeo.org<br>
<b>Kopia:</b> postgis-users@lists.osgeo.org<br>
<b>Ämne:</b> Re: [postgis-users] Help with SQL<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">Hi Paul,<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif""> <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">no need for subqueries here. Do a spatial join between polygon and points and start you query with DISTINCT ON (poly.id) poly.id, o.height etc.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif""> <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">DISTINCT ON (poly.id) will get you one row from the JOIN result per polygon. Which one it picks you can control with the final ORDER BY clause - in your case the height values
of points.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">The first column in the ORDER BY must be the "group" of your DISTINCT ON and then the height value.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif""> <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">cheers<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">Felix<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">
<o:p></o:p></span></p>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">
<o:p></o:p></span></p>
<div style="border:none;border-left:solid #C3D9E5 1.5pt;padding:0cm 0cm 0cm 8.0pt;margin-left:7.5pt;margin-top:7.5pt;margin-right:3.75pt;margin-bottom:3.75pt;word-wrap: break-word;-webkit-nbsp-mode: space;-webkit-line-break: after-white-space" name="quote">
<div style="margin-bottom:7.5pt">
<p class="MsoNormal"><b><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">Gesendet:</span></b><span style="font-size:9.0pt;font-family:"Verdana","sans-serif""> Montag, 09. März 2020 um 10:21 Uhr<br>
<b>Von:</b> paul.malm@lfv.se<br>
<b>An:</b> postgis-users@lists.osgeo.org<br>
<b>Betreff:</b> [postgis-users] Help with SQL<o:p></o:p></span></p>
</div>
<div name="quoted-content">
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Hi,<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">I have 2 tables:<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">obstacles (point layer) with a column height_m (INTEGER) and a
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">polyobstacles (polygon layer)<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">the point layer has obstacles all over the map and the polygon layer has some polygons containing some of the obstacle points.<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Now I would like to select the highest obstacle in each polygon. If there is several points with the same highest height a random obstacle of those highest shall be selected.<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Is there someone that can get me on the track for such a query, I’m losing myself in subqueries?<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Kind regards,<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Paul<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:9.0pt"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:9.0pt">
<img width="132" height="107" id="Bild_x0020_1" src="cid:image001.jpg@01D5F6CB.A7C5F4E0" alt="2_LFV_engelsk_96"></span><o:p></o:p></p>
<table class="MsoNormalTable" border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse">
<tbody>
<tr style="height:36.85pt">
<td width="420" valign="top" style="width:315.0pt;padding:0cm 0cm 0cm 2.25pt;height:36.85pt">
<p class="MsoNormal" style="margin-top:2.0pt;mso-margin-bottom-alt:auto;line-height:10.0pt">
<span style="font-size:9.0pt;color:#333333"> </span><o:p></o:p></p>
<p class="MsoNormal" style="margin-top:2.0pt;mso-margin-bottom-alt:auto;line-height:10.0pt">
<b><span style="font-size:9.0pt;color:#333333"> Paul Malm</span></b><br>
<br>
<span style="font-size:9.0pt;color:#333333"> Operations AIM</span><o:p></o:p></p>
</td>
</tr>
<tr style="height:24.1pt">
<td width="420" valign="top" style="width:315.0pt;padding:0cm 0cm 0cm 2.25pt;height:24.1pt">
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;line-height:9.0pt">
<span style="font-size:9.0pt;color:#333333"> Direct +46 (0)8 797 70 23 Mobile: +46 (0)708 601115<br>
paul.malm@lfv.se </span><br>
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;line-height:9.0pt">
<span style="font-size:9.0pt;color:#333333"> Mail & Visit: Löjtnantsgatan 25, 115 50 Stockholm, Sweden</span><o:p></o:p></p>
</td>
</tr>
<tr>
<td width="420" valign="top" style="width:315.0pt;padding:0cm 0cm 0cm 2.25pt">
<p class="MsoNormal" style="margin-top:2.0pt;mso-margin-bottom-alt:auto;line-height:9.0pt">
<span style="font-size:9.0pt;color:#333333"> Unit phone: +46 (0)8 797 70 20 <br>
<a href="http://www.lfv.se/" target="_blank">www.lfv.se</a></span><o:p></o:p></p>
</td>
</tr>
<tr style="height:3.7pt">
<td width="420" valign="top" style="width:315.0pt;padding:0cm 0cm 0cm 2.25pt;height:3.7pt">
<p class="MsoNormal" style="margin-top:6.0pt;mso-margin-bottom-alt:auto;mso-line-height-alt:3.7pt">
<span style="font-size:9.0pt;color:#00CC00"> Please consider the enviroment before printing this e-mail message.</span><o:p></o:p></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:9.0pt"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
</div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Verdana","sans-serif"">_______________________________________________ postgis-users mailing list postgis-users@lists.osgeo.org
<a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" target="_blank">
https://lists.osgeo.org/mailman/listinfo/postgis-users</a><o:p></o:p></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>