From: | Mario Splivalo <mario(dot)splivalo(at)megafon(dot)hr> |
---|---|
To: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Get the max viewd product_id for user_id |
Date: | 2010-12-05 16:48:10 |
Message-ID: | 4CFBC24A.90708@megafon.hr |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
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
>
It does, yes. Actually, pretty silly of me not to implement it that way,
thank you.
Since I already have the view, the query now looks like this:
select
x.user_id,
y.product_id,
x.views
from (
select
user_id,
max(views) as views
from
v_views
group by
user_id
) as x
inner join v_views as y
on x.user_id=y.user_id and x.views=y.views
And CTEs would also help here :)
Mario
From | Date | Subject | |
---|---|---|---|
Next Message | Mario Splivalo | 2010-12-05 16:57:17 | Re: Get the max viewd product_id for user_id |
Previous Message | Jasen Betts | 2010-12-03 23:20:33 | Re: Union Question |