Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 488AA1337B50 for ; Sun, 5 Dec 2010 12:57:26 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 70418-01 for ; Sun, 5 Dec 2010 16:57:19 +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 0DDF31337B71 for ; Sun, 5 Dec 2010 12:57:19 -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 605121FE44; Sun, 5 Dec 2010 17:57:18 +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 oB5GvI5T024492; Sun, 5 Dec 2010 17:57:18 +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 syk2b8bs8-1; Sun, 05 Dec 2010 17:57:18 +0100 A-Original-Client-UID: 188.129.106.69 splivalo Received: from [192.168.10.1] (cpe-188-129-106-69.dynamic.amis.hr [188.129.106.69]) (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 DD5EA27C; Sun, 5 Dec 2010 17:57:17 +0100 (CET) Message-ID: <4CFBC46D.1080105@megafon.hr> Date: Sun, 05 Dec 2010 17:57:17 +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: Jayadevan M CC: pgsql-sql@postgresql.org Subject: Re: Get the max viewd product_id for user_id References: <4CF8CC26.70206@megafon.hr> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; 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-05_06:2010-12-04, 2010-12-05, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx engine=6.0.2-1010190000 definitions=main-1012050079 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/26 X-Sequence-Number: 34862 On 12/03/2010 12:40 PM, Jayadevan M wrote: > Hello, > >> 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 >> > Does this work faster? > select x.user_id,y.product_id,x.count from > (select user_id, max(count ) as count from (select user_id,product_id, > count(*) as count from viewlog group by user_id,product_id) as x group by > user_id > ) as x inner join > (select user_id,product_id, count(*) as count1 from viewlog group by > user_id,product_id ) as y > on x.user_id=y.user_id and x.count=y.count1 The issue in both approaches is that if I have two product_ids that are viewed same number of times and share the first place as most viewed products by that user, I'll get only one of them (LIMIT 1 OR MAX() can only return one row :). I don't see how I can sort this out with elegance in SQL. Mario