Re: Problems with group by ... order by

From: Bricklen Anderson <BAnderson(at)PresiNET(dot)com>
To: "John D(dot) Burger" <john(at)mitre(dot)org>
Cc: PostgreSQL general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Problems with group by ... order by
Date: 2005-10-05 19:05:47
Message-ID: 4344240B.3050101@PresiNET.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

John D. Burger wrote:
> I can't figure out why the following doesn't work:
>
> select
> (case
> when count1 < 300 then 'Other'
> else country1
> end) as country2,
> sum(count1) as count2
> from (select coalesce(country, 'None') as country1, count(*) as count1
> from userProfiles group by country1) as counts1
> group by country2
> order by (country2 = 'Other'), count2 desc
>

Do either of these work for you? Note, completely untested, and just off the top
of my head.

select
(case
when count1 < 300 then 'Other'
else country1
end) as country2,
sum(count1) as count2
from (select coalesce(country, 'None') as country1, count(*) as count1
from userProfiles group by country1) as counts1
group by country2
order by (1 = 'Other'), count2 desc

select
(case
when count1 < 300 then null
else country1
end) as country2,
sum(count1) as count2
from (select coalesce(country, 'None') as country1, count(*) as count1
from userProfiles group by country1) as counts1
group by country2
order by (1 is null), count2 desc

--
_______________________________

This e-mail may be privileged and/or confidential, and the sender does
not waive any related rights and obligations. Any distribution, use or
copying of this e-mail or the information it contains by other than an
intended recipient is unauthorized. If you received this e-mail in
error, please advise me (by return e-mail or otherwise) immediately.
_______________________________

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Dennis Jenkins 2005-10-05 19:10:54 SPI_prepare, SPI_execute_plan do not return rows when using parameters
Previous Message Josh Berkus 2005-10-05 19:00:48 Re: [PERFORM] Text/Varchar performance...