Re: Formatting Functions and Group By

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Formatting Functions and Group By
Дата
Msg-id 24390.1081877598@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Formatting Functions and Group By  (Terry Brick <terry_brick2000@yahoo.com>)
Ответы Re: Formatting Functions and Group By
Список pgsql-sql
Terry Brick <terry_brick2000@yahoo.com> writes:
> I'm porting a bunch of queries from MySQL to Postgres 7.4 and am having a problem with one
> particular area.  For example, a query like this works in MySQL:

> select
>   to_char(myCol,'Mon YY')
> from
>   myTable
> group by
>   to_char(myCol,'MM YYYY')
> order by
>   to_char(myCol,'MM YYYY')

Ah, good ol' MySQL :-( ... let the user do what he wants whether the
result is well defined or not ...

I'd suggest doing the grouping/ordering numerically rather than
textually.  For instance,

select to_char(date_trunc('month', myCol), 'Mon YY')
from myTable
group by date_trunc('month', myCol)
order by date_trunc('month', myCol);

Now this assumes you really want a time-based ordering, which the quoted
example doesn't give --- you've got month sorting to the left of year,
is that really what you want?  If it is then you'd need to go

group by date_trunc('month', myCol)
order by to_char(date_trunc('month', myCol), 'MM YYYY')
        regards, tom lane


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

Предыдущее
От: Bruno Wolff III
Дата:
Сообщение: Re: Formatting Functions and Group By
Следующее
От: Greg Stark
Дата:
Сообщение: Re: trigger/for key help