Get the max viewd product_id for user_id - Mailing list pgsql-sql

From Mario Splivalo
Subject Get the max viewd product_id for user_id
Date
Msg-id 4CF8CC26.70206@megafon.hr
Whole thread Raw
Responses Re: Get the max viewd product_id for user_id
List 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 by date:

Previous
From: Jayadevan M
Date:
Subject: Re: Calculate next event date based on instance of the day of week
Next
From: Jayadevan M
Date:
Subject: Re: Get the max viewd product_id for user_id