Get the max viewd product_id for user_id

Поиск
Список
Период
Сортировка
От Mario Splivalo
Тема Get the max viewd product_id for user_id
Дата
Msg-id 4CF8CC26.70206@megafon.hr
обсуждение исходный текст
Ответы Re: Get the max viewd product_id for user_id
Список pgsql-sql
I have a log-table where I record when some user_id has viewed some 
product_id:

CREATE TABLE viewlog (user_id integer,product_id integer,view_timestamp timestamp with time zone
)

Now, I would like to get result that gives me, for each user_id, 
product_id of the product he/she viewed the most time, with the number 
of views.

The 'issue' is I need this running on postgres 8.0.

I went this way, but for a large number of user_id's, it's quite slow:

CREATE VIEW v_views ASSELECT user_id, product_id, count(*) as viewsFROM viewlogGROUP BY user_id, product_id

SELECTDISTINCT user_id,(SELECT product_id FROM v_views inn WHERE inn.user_id = out.user_id 
ORDER BY views DESC LIMIT 1) as product_id,(SELECT views FROM v_views inn WHERE inn.user_id = out.user_id ORDER BY 
views DESC LIMIT 1) as views
FROMv_views out

Mario


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

Предыдущее
От: Jayadevan M
Дата:
Сообщение: Re: Calculate next event date based on instance of the day of week
Следующее
От: Jayadevan M
Дата:
Сообщение: Re: Get the max viewd product_id for user_id