pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jayadevan M <Jayadevan.Maymala@ibsplc.com>
To: Mario Splivalo <mario.splivalo@megafon.hr>
Cc: pgsql-sql@postgresql.org
Cc: pgsql-sql-owner@postgresql.org
Subject: Re: Get the max viewd product_id for user_id
Date: Fri, 3 Dec 2010 17:10:31 +0530
Message-ID: <OF16FA4770.DCF7942F-ON652577EE.003F9FF3-652577EE.0040227D@ibsplc.com> (raw)
In-Reply-To: <4CF8CC26.70206@megafon.hr>
References: <4CF8CC26.70206@megafon.hr>

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."








view thread (6+ messages)  latest in thread

Message-ID: <OF16FA4770.DCF7942F-ON652577EE.003F9FF3-652577EE.0040227D@ibsplc.com>
Permalink:  ../OF16FA4770.DCF7942F-ON652577EE.003F9FF3-652577EE.0040227D@ibsplc.com/
Also on:    postgresql.org/message-id/OF16FA4770.DCF7942F-ON652577EE.003F9FF3-652577EE.0040227D@ibsplc.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: Jayadevan.Maymala@ibsplc.com, mario.splivalo@megafon.hr, pgsql-sql-owner@postgresql.org
  Subject: Re: Get the max viewd product_id for user_id
  In-Reply-To: <OF16FA4770.DCF7942F-ON652577EE.003F9FF3-652577EE.0040227D@ibsplc.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox