Search Postgresql Archives

How do I write this query? Distinct, Group By, Order By?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 Hi There,

I have a table looks like this:

(order_id, user_id, order_time)

One user_id can have multiple orders with order_id as the primary key, now I want to get a list of users, ordered by their latest order respectively, for example, if user A has two orders, one on today, the other a month ago, and user B has one order a week ago, then the result should be

A
B

how do I do it? I tried various ways of SELECT with Distinct, Group By, Order By, but was hit by either "column must appear in the GROUP BY clause or be used in an aggregate function", or "for SELECT DISTINCT, ORDER BY expressions must appear in select list" every time.

Is it possible to do it? Is it possible to do it in one none-nested query?


Many Thanks!


--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux