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 AS
        SELECT user_id, product_id, count(*) as views
        FROM viewlog
        GROUP BY user_id, product_id

SELECT
        DISTINCT 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
FROM
        v_views out


        Mario

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

Reply via email to