pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pivot query with count
5+ messages / 2 participants
[nested] [flat]

* pivot query with count
@ 2013-04-12 21:14 Tony Capobianco <tony.capobianco1@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Tony Capobianco @ 2013-04-12 21:14 UTC (permalink / raw)
  To: pgsql-sql

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?

Thanks.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pivot query with count
@ 2013-04-13 00:28 Tony Capobianco <tony.capobianco1@gmail.com>
  2013-04-13 03:26 ` Re: pivot query with count David Johnston <polobo@yahoo.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tony Capobianco @ 2013-04-13 00:28 UTC (permalink / raw)
  To: pgsql-sql

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.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: pivot query with count
  2013-04-13 00:28 pivot query with count Tony Capobianco <tony.capobianco1@gmail.com>
@ 2013-04-13 03:26 ` David Johnston <polobo@yahoo.com>
  2013-04-13 03:29   ` Re: pivot query with count David Johnston <polobo@yahoo.com>
  0 siblings, 1 reply; 5+ messages in thread

From: David Johnston @ 2013-04-13 03:26 UTC (permalink / raw)
  To: pgsql-sql

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.


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: pivot query with count
  2013-04-13 00:28 pivot query with count Tony Capobianco <tony.capobianco1@gmail.com>
  2013-04-13 03:26 ` Re: pivot query with count David Johnston <polobo@yahoo.com>
@ 2013-04-13 03:29   ` David Johnston <polobo@yahoo.com>
  2013-04-13 22:05     ` Re: pivot query with count Tony Capobianco <tony.capobianco1@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: David Johnston @ 2013-04-13 03:29 UTC (permalink / raw)
  To: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: pivot query with count
  2013-04-13 00:28 pivot query with count Tony Capobianco <tony.capobianco1@gmail.com>
  2013-04-13 03:26 ` Re: pivot query with count David Johnston <polobo@yahoo.com>
  2013-04-13 03:29   ` Re: pivot query with count David Johnston <polobo@yahoo.com>
@ 2013-04-13 22:05     ` Tony Capobianco <tony.capobianco1@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Tony Capobianco @ 2013-04-13 22:05 UTC (permalink / raw)
  To: David Johnston <polobo@yahoo.com>; +Cc: 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@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
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2013-04-13 22:05 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-04-12 21:14 pivot query with count Tony Capobianco <tony.capobianco1@gmail.com>
2013-04-13 00:28 pivot query with count Tony Capobianco <tony.capobianco1@gmail.com>
2013-04-13 03:26 ` David Johnston <polobo@yahoo.com>
2013-04-13 03:29   ` David Johnston <polobo@yahoo.com>
2013-04-13 22:05     ` Tony Capobianco <tony.capobianco1@gmail.com>

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