Re: How to find out top 3 records in each location

Поиск
Список
Период
Сортировка
От Richard Broersma Jr
Тема Re: How to find out top 3 records in each location
Дата
Msg-id 452822.24102.qm@web31801.mail.mud.yahoo.com
обсуждение исходный текст
Ответ на How to find out top 3 records in each location  ("wen tseng" <went@hteamericas.com>)
Ответы Re: How to find out top 3 records in each location
Список pgsql-sql
> On PostgreSQL, I have a table like this:
> 
> Item     Location    Sales
>  A            X             10
>  B            X              6
>  C            Y              3
>  D            Y              8
>  E            Y              15
>  F            Y             11
> 
> I'd like to find out top 3 items in each location and put those 3 items as colum values like
> this:
> 
> Location    Top1   Top2   Top3
>   X              A        B
>   Y             E         F        D
> 
> Since PostgreSQL doesn't support TOP, how can I do to get this result?
> Any solution will be appreciated.


Here is what I came up with.  However, I am sure there maybe a better answer.

SELECT A1.location,( select sales  from sales  where location = A1.location  order by sales desc   limit 1) as TOP1,(
selectsales  from sales  where location = A1.location  order by sales desc   limit 1 offset 1) as Top2,( select sales
fromsales  where location = A1.location  order by sales desc  limit 1 offset 2) as Top3
 


FROM( SELECT location  from sales  group by location) AS A1
;


Regards,

Richard Broersma Jr.


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

Предыдущее
От: Andrew Sullivan
Дата:
Сообщение: Re: How to find out top 3 records in each location
Следующее
От: "lms"
Дата:
Сообщение: Re: How convert UNICODE