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

Re: [SQL] Clarity on how LOCK interacts with INHERIT

2013-04-12 Thread Simon Riggs
On 12 April 2013 21:59, Robins Tharakan wrote: > postgres=# CREATE TABLE lock_tbl6 (b BIGINT) INHERITS (lock_tbl5); > CREATE TABLE > postgres=> LOCK TABLE lock_tbl6 * IN access EXCLUSIVE MODE; > LOCK TABLE > postgres=> LOCK TABLE lock_tbl5 IN access EXCLUSIVE MODE; > ERROR: permission denied f

[SQL] Clarity on how LOCK interacts with INHERIT

2013-04-12 Thread Robins Tharakan
Hi, The following when run in PostgreSQL 9.2.x seems to tell that if I have two tables A and B, such that if B inherits A, then, even if I don't have any rights on A, I can get an ACCESS EXCLUSIVE LOCK on Table A via Table B. This isn't necessarily wrong, I just want be sure that this is what Pos

Re: [SQL] Restrict FOREIGN KEY to a part of the referenced table

2013-04-12 Thread Richard Broersma
On Thu, Apr 11, 2013 at 12:55 AM, Matthias Nagel wrote: > > Working solution: > > CREATE TABLE child ( > id SERIAL NOT NULL, > parent_id INT NOT NULL, > parent_discriminator INT NOT NULL DEFAULT 42, > attribute2 VARCHAR, > ..., > FOREIGN KEY ( parent_id, parent_discriminator ) REFEREN

Re: [SQL] Restrict FOREIGN KEY to a part of the referenced table

2013-04-12 Thread Jasen Betts
On 2013-04-11, Matthias Nagel wrote: > Hello, > > is there any best practice method how to create a foreign key that only > allows values from those rows in the referenced table that fulfill an > additional condition? tes. make the key wide enough to capture this state. I dom't like it either.

Re: [SQL] Advice for index design

2013-04-12 Thread Jasen Betts
On 2013-04-10, JORGE MALDONADO wrote: > Our application offers a catalog of artists where a user can select a range > of birthdays and/or sex. For example, a user can get an artists catalog for > those male artists who were born between May 1, 1970 and May 1, 1990 > ordered by birthday and, with