Re: pivot query with count

From: Tony Capobianco <tony(dot)capobianco1(at)gmail(dot)com>
To: David Johnston <polobo(at)yahoo(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: pivot query with count
Date: 2013-04-13 22:05:32
Message-ID: CABx31i4E52vtWjCwf-qQGbNC2vxE+xFPfhsBKBiFbTTOsp7xPQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thank you very much for your response. However, I'm unclear what you want
me to substitute for sum(...)?

select '1' as "num_ads", sum(...)
from
(select a.userid from
user_event_stg2 a, user_region b
where a.userid = b.userid
and b.region_code = 1000
and a.messagetype = 'impression'
group by a.userid having count(a.userid) = 1)
group by num_ads;

I was able to eliminate that sub-select per your recommendation. That
makes things a bit easier.

Thanks.

On Fri, Apr 12, 2013 at 11:29 PM, David Johnston <polobo(at)yahoo(dot)com> wrote:

> My prior comment simply answers your question. You likely can rewrite
> your
> query so that a separate grouping layer is not needed (or rather the group
> by would exist in the main query and you minimize the case/sub-select
> column
> queries and use aggregates and case instead).
>
> David J.
>
>
>
>
> --
> View this message in context:
> http://postgresql.1045698.n5.nabble.com/pivot-query-with-count-tp5752072p5752078.html
> Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
>
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Mark Stosberg 2013-04-16 16:24:00 Peer-review requested of soft-delete scheme
Previous Message David Johnston 2013-04-13 03:29:15 Re: pivot query with count