Thread: pivot query with count

pivot query with count

From
Tony Capobianco
Date:
The following is my code and results:
 
select '1' "num_ads",
     (case when r.region_code = 1000 then (
          select count(*) from  (
           select userid from user_event_stg2 where userid in (
            select userid from user_region where region_code = 1000)
             and messagetype = 'impression' group by userid
              having count(userid) = 1) as foo) else 0 end) as "NorthEast",
     (case when r.region_code = 2000 then (
          select count(*) from  (
           select userid from user_event_stg2 where userid in (
            select userid from user_region where region_code = 2000)
             and messagetype = 'impression' group by userid
              having count(userid) = 1) as foo) else 0 end) as "NorthWest",
     (case when r.region_code = 3000 then (
          select count(*) from  (
           select userid from user_event_stg2 where userid in (
            select userid from user_region where region_code = 3000)
             and messagetype = 'impression' group by userid
              having count(userid) = 1) as foo) else 0 end) as "SouthEast",
     (case when r.region_code = 4000 then (
          select count(*) from  (
           select userid from user_event_stg2 where userid in (
            select userid from user_region where region_code = 4000)
             and messagetype = 'impression' group by userid
              having count(userid) = 1) as foo) else 0 end) as "SouthWest",
     (case when r.region_code = 5000 then (
          select count(*) from  (
           select userid from user_event_stg2 where userid in (
            select userid from user_region where region_code = 5000)
             and messagetype = 'impression' group by userid
              having count(userid) = 1) as foo) else 0 end) as "Middle of Nowhere"
from user_region u, region r
where u.region_code = r.region_code
group by r.region_code;
 
num_ads | NorthEast | NorthWest | SouthEast | SouthWest | Middle of Nowhere
---------+-----------+-----------+-----------+-----------+-------------------
 1       |         0 |         0 |      3898 |         0 |                 0
 1       |      3895 |         0 |         0 |         0 |                 0
 1       |         0 |      3873 |         0 |         0 |                 0
 1       |         0 |         0 |         0 |      3915 |                 0
 
How can I get this output on to a single line?
 
num_ads | NorthEast | NorthWest | SouthEast | SouthWest | Middle of Nowhere
---------+-----------+-----------+-----------+-----------+-------------------
 1       |    3895 |    3873 |     3898 |    3915 |                 0
Thanks.

Re: pivot query with count

From
David Johnston
Date:
SELECT num_ads, sum(...), sum(...), ....
FROM ( your query here )
GROUP BY num_ads;


BTW, While "SELECT '1' "num_ads" is valid syntax I recommend you use the
"AS" keyword.  '1' AS "num_ads"

David J.




--
View this message in context: http://postgresql.1045698.n5.nabble.com/pivot-query-with-count-tp5752072p5752077.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.



Re: pivot query with count

From
David Johnston
Date:
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.



Re: pivot query with count

From
Tony Capobianco
Date:
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@yahoo.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@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql