Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D8D9D1337BBE for ; Fri, 3 Dec 2010 07:42:37 -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 35617-10; Fri, 3 Dec 2010 11:42:31 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mx1.ibsplc.com (mx1.ibsplc.com [202.177.44.5]) by mail.postgresql.org (Postfix) with ESMTP id 721C41337B2F; Fri, 3 Dec 2010 07:42:29 -0400 (AST) X-IronPort-AV: E=Sophos;i="4.59,292,1288549800"; d="scan'208";a="20570889" Received: from unknown (HELO Mail1.ibsplc.com) ([192.168.0.8]) by mx2.ibsplc.com with ESMTP; 03 Dec 2010 17:10:33 +0530 In-Reply-To: <4CF8CC26.70206@megafon.hr> References: <4CF8CC26.70206@megafon.hr> X-Disclaimed: 63966 To: Mario Splivalo Cc: pgsql-sql@postgresql.org, pgsql-sql-owner@postgresql.org MIME-Version: 1.0 Subject: Re: Get the max viewd product_id for user_id X-KeepSent: 16FA4770:DCF7942F-652577EE:003F9FF3; type=4; name=$KeepSent X-Mailer: Lotus Notes Release 8.5 December 05, 2008 Message-ID: From: Jayadevan M Date: Fri, 3 Dec 2010 17:10:31 +0530 X-MIMETrack: Serialize by Router on Mail1/IBS(Release 8.5.1FP1|January 05, 2010) at 12/03/2010 05:10:33 PM, Serialize complete at 12/03/2010 05:10:33 PM Content-Type: text/plain; charset="US-ASCII" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201012/14 X-Sequence-Number: 34850 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 Regards, Jayadevan DISCLAIMER: "The information in this e-mail and any attachment is intended only for the person to whom it is addressed and may contain confidential and/or privileged material. If you have received this e-mail in error, kindly contact the sender and destroy all copies of the original communication. IBS makes no warranty, express or implied, nor guarantees the accuracy, adequacy or completeness of the information contained in this email or any attachment and is not liable for any errors, defects, omissions, viruses or for resultant loss or damage, if any, direct or indirect."