Re: Problems with group by ... order by

Поиск
Список
Период
Сортировка
От Bricklen Anderson
Тема Re: Problems with group by ... order by
Дата
Msg-id 4344240B.3050101@PresiNET.com
обсуждение исходный текст
Ответ на Problems with group by ... order by  ("John D. Burger" <john@mitre.org>)
Список 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.
_______________________________

В списке pgsql-general по дате отправления:

Предыдущее
От: Josh Berkus
Дата:
Сообщение: Re: [PERFORM] Text/Varchar performance...
Следующее
От: Dennis Jenkins
Дата:
Сообщение: SPI_prepare, SPI_execute_plan do not return rows when using parameters