Re: efficient count/join query

From: Tomasz Myrta <jasiek(at)klaster(dot)net>
To: Gary Stainburn <gary(dot)stainburn(at)ringways(dot)co(dot)uk>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: efficient count/join query
Date: 2003-02-07 14:38:59
Message-ID: 3E43C503.6010508@klaster.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Gary Stainburn wrote:
> On Friday 07 Feb 2003 1:26 pm, Tomasz Myrta wrote:
>
>>Gary Stainburn wrote:
>>

<cut>
>
> Many appologoes Tomasz.
No problem
>
> Because your select *LOOKED* wrong to me, I didn't even try it. Upon looking
> at it again I can see what you're doing. When I tried, it complained about
> the counts and grouping, so I moved the count(*) to a sub-select and changed
> the coalesce and it's working.
There were 2 bugs in this query:
- in group by - add field hcount
- counting when any roster doesn't exist for some history. Here is (I suppose) final query:

create view some_view as
select
coalesce(hjid,rjid) as jid,
coalesce(hsid,rsid) as sid,
coalesce(hcount,0)+
sum(case when rjid is not null then 1 else 0 end) as hcount
from
history
full outer join roster on (hjid=rjid and hsid=rosid)
group by hjid,rjid,hsid,rosid,hcount;

Tomasz Myrta

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Daniel Schuchardt 2003-02-07 16:52:34 Re: "function has no parameter $1" - help.
Previous Message Gary Stainburn 2003-02-07 14:21:14 Re: efficient count/join query