From: Mario Splivalo <mario.splivalo@megafon.hr>
To: Jayadevan M <Jayadevan.Maymala@ibsplc.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Get the max viewd product_id for user_id
Date: Sun, 05 Dec 2010 17:57:17 +0100
Message-ID: <4CFBC46D.1080105@megafon.hr> (raw)
In-Reply-To: <OF16FA4770.DCF7942F-ON652577EE.003F9FF3-652577EE.0040227D@ibsplc.com>
References: <4CF8CC26.70206@megafon.hr>
<OF16FA4770.DCF7942F-ON652577EE.003F9FF3-652577EE.0040227D@ibsplc.com>
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
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: mario.splivalo@megafon.hr, Jayadevan.Maymala@ibsplc.com
Subject: Re: Get the max viewd product_id for user_id
In-Reply-To: <4CFBC46D.1080105@megafon.hr>
* 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