Re: [SQL] pivot query with count

2013-04-13 Thread Tony Capobianco
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.user

Re: [SQL] pivot query with count

2013-04-12 Thread David Johnston
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 th

Re: [SQL] pivot query with count

2013-04-12 Thread David Johnston
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-cou

[SQL] pivot query with count

2013-04-12 Thread Tony Capobianco
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 messagetyp

[SQL] pivot query with count

2013-04-12 Thread Tony Capobianco
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 messagetyp