Re : Re : Query "top 10 and others"

From: Edson Richter <edsonrichter(at)hotmail(dot)com>
To: David Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org <pgsql-general(at)postgresql(dot)org>
Subject: Re : Re : Query "top 10 and others"
Date: 2014-07-05 13:12:59
Message-ID: BLU436-SMTP240787FA3A6995D56099C89CF030@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<span style="font-size:10pt;"><p style="margin-top:0;margin-bottom:0;">Thanks!</p><p style="margin-top:0;margin-bottom:0;">I'll investigate (explain) performance for both versions.</p><p style="margin-top:0;margin-bottom:0;">&nbsp;</p><dev3_jjy>Atenciosamente,<br><br>Edson Richter&nbsp;</dev3_jjy><br><br>------ Mensagem original ------<br><b>De:&nbsp;</b>David Johnston <david(dot)g(dot)johnston(at)gmail(dot)com><br><b>Data:&nbsp;</b>04/07/2014 21h20<br><b>Para:&nbsp;</b>Edson Richter;<br><b>Cc:</b>pgsql-general(at)postgresql(dot)org;<br><b>Assunto:</b>Re: Re : [GENERAL] Query &quot;top 10 and others&quot;<br><br><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span style="font-size:10pt"><pre>&gt; with QRY as (select C1.country, C1.state, sum(C1.population)
&gt; &nbsp;&nbsp;from places C1
&gt; &nbsp;&nbsp;group by 1, 2
&gt; &nbsp;&nbsp;&nbsp;order by 3 DESC
&gt; &nbsp;&nbsp;limit 10)
&gt;
&gt; select * from QRY
&gt; union
&gt; select 'others' as &quot;country&quot;, '' as &quot;state&quot;, sum(population)
&gt; &nbsp;&nbsp;from places
&gt; &nbsp;where not exists (select 1 from QRY where country = QRY.country and state
&gt; = QRY.state)
&gt;&nbsp;</pre></span></blockquote><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">(not tested)</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">
​with QRY as ( SELECT country, state, sum(population) as st_pop FROM places GROUP BY country, state )</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">, u1 AS ​( SELECT country, state, st_pop FROM QRY ORDER BY st_pop DESC LIMIT 10 )</div>
<div class="gmail_default" style="font-family:arial,helvetica,sans-serif">, u2 AS ( SELECT 'other' AS country, '' AS state, sum(st_pop) FROM QRY WHERE NOT EXISTS (</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">
SELECT 1 FROM u1 WHERE (QRY.country, QRY.state) = (u1.country, u1,state)</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">)</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">
SELECT * FROM u1</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">UNION ALL</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">SELECT * FROM u2</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">
;</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">David J.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div></div></div></div>

</span>

Attachment Content-Type Size
unknown_filename text/html 2.7 KB

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bob Jolliffe 2014-07-05 13:34:51 Re: Very high latency, low bandwidth replication
Previous Message Augori 2014-07-05 11:58:45 Re: Failed to build transaction for opengeo-server