Re: [GENERAL] Re: sql question

From: Herbert Liechti <Herbert(dot)Liechti(at)thinx(dot)ch>
To: postgres <pgsql-general(at)postgreSQL(dot)org>
Cc: Ed Loehr <ELOEHR(at)austin(dot)rr(dot)com>
Subject: Re: [GENERAL] Re: sql question
Date: 1999-11-18 21:09:27
Message-ID: 38346B06.61F74C93@thinx.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ed Loehr wrote:

> > From: Herbert Liechti <Herbert(dot)Liechti(at)thinx(dot)ch>
> > Subject: sql question
> >
> > I have a sql problem which I can't solve. The following table is defined
> >
> > create table AdressGroup
> > (
> > AdrGroup_Id INTEGER NOT NULL
> > DEFAULT NEXTVAL('adrverw_id_seq'),
> > Zeit DATETIME NOT NULL,
> > Group_Id INTEGER NOT NULL,
> > Adress_Id INTEGER NOT NULL
> > );
> >
> > The table assigns adresses to adressgroups.
> >
> > I'd like to select the adresses which belongs to one specific adressGroup and to no other group. If an adress has more than one entry in the AdressGroup
> > table it should not be in the projection.
> >
> > I tried the following:
> >
> > SELECT * FROM adressGroup
> > WHERE Group_Id = 6
> > AND EXISTS( SELECT AdrGroup_Id FROM adressGroup ag_alias
> > WHERE adressGroup.AdrGroup_Id = ag_alias.AdrGroup_Id
> > GROUP BY AdrGroup_Id HAVING COUNT(*) = 1 );
> >
> > When I do this I get the following error:
> > ERROR: rewrite: aggregate column of view must be at rigth side in qual
> > ERROR: rewrite: aggregate column of view must be at rigth side in qual
> >
> > Does anybody knows the solution? Thanks
> >
>
> Not sure about the error above, other than the apparent typos with "adressGroup", but I think you can get what you're after with this:
>
> SELECT *
> FROM AdressGroup ag
> WHERE ag.Group_Id = 6 AND NOT EXISTS (
> SELECT AdrGroup_Id
> FROM AddressGroup ag2
> WHERE ag2.AdrGroup_Id = ag.AdrGroup_Id AND ag2.Group_Id <> 6);

The query does not produce the estimated result :-/( It returns all records from
adressgroup 6. It seems that the problem can not be solved in a single sql-statement.
I assume that the above statement joins the table with the alias table with the
subset of adress group 6.
Probably I do it with a temporary table:

Create temp table tempGroup(adrGroup_id integer, groupcounter integer);
INSERT INTO tempGroup
SELECT adrGroup_Id, count(*) FROM adrGroup GROUP BY 1 HAVING COUNT(*) = 1);
SELECT * from adrGroup, tempGroup
WHERE adrGroup.adrGroup_id = tempGroup.adrGroup_id
AND adrGroup.group_id = 7;

This is working but without the desired performance :-(

Thanks anyway
Herbie

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Herbert Liechti E-Mail: Herbert(dot)Liechti(at)thinx(dot)ch
ThinX networked business services Stahlrain 10, CH-5200 Brugg
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Herbert Liechti 1999-11-18 21:13:10 Re: [GENERAL] postmaster problem
Previous Message Martin Weinberg 1999-11-18 21:03:25 Re: [GENERAL] How to efficiently update many records at once