Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id AB2EB1337BBE for ; Fri, 3 Dec 2010 06:53:35 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 14226-07 for ; Fri, 3 Dec 2010 10:53:29 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from out.mail.amis.hr (out1.mail.amis.hr [188.129.0.4]) by mail.postgresql.org (Postfix) with ESMTP id 475991337BBC for ; Fri, 3 Dec 2010 06:53:28 -0400 (AST) Received: from out.fsec.sm.srv.amis.hr (out.fsec.sm.srv.amis.hr [192.168.106.22]) by out.mail.amis.hr (Postfix) with ESMTP id A13C020148 for ; Fri, 3 Dec 2010 11:53:26 +0100 (CET) Received: from pps.filterd (out [127.0.0.1]) by out.fsec.sm.srv.amis.hr (8.14.3/8.14.3) with SMTP id oB3AlL6F009143 for ; Fri, 3 Dec 2010 11:53:26 +0100 Received: from smtp.amis.hr (smtp.sm.srv.amis.hr [192.168.106.39]) by out.fsec.sm.srv.amis.hr with ESMTP id sxh7s077a-1 for ; Fri, 03 Dec 2010 11:53:26 +0100 A-Original-Client-UID: 188.129.71.72 splivalo Received: from [192.168.10.1] (cpe-188-129-71-72.dynamic.amis.hr [188.129.71.72]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: splivalo) by smtp.amis.hr (Postfix) with ESMTPSA id 3F3DF23C for ; Fri, 3 Dec 2010 11:53:26 +0100 (CET) Message-ID: <4CF8CC26.70206@megafon.hr> Date: Fri, 03 Dec 2010 11:53:26 +0100 From: Mario Splivalo User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.15) Gecko/20101027 Lightning/1.0b1 Thunderbird/3.0.10 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Get the max viewd product_id for user_id Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.2.15, 1.0.148, 0.0.0000 definitions=2010-12-03_08:2010-12-03, 2010-12-03, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=1 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx engine=6.0.2-1010190000 definitions=main-1012030020 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201012/13 X-Sequence-Number: 34849 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