Re: [GENERAL] sql question - Mailing list pgsql-general

From Herbert Liechti
Subject Re: [GENERAL] sql question
Date
Msg-id 3833F1E1.861CA6AB@thinx.ch
Whole thread Raw
List pgsql-general
Alain.Tesio@sip.fr wrote:

> > >  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);
>
> One last try : your query looks pretty strange, maybe you should design it again
> from scratch :
> if I understand well, you want the rows in adressGroup with :
>
> - Group_Id=6
> - adrGroup_Id being unique
>
> So why not :
>
> SELECT * FROM adressGroup
> WHERE Group_Id = 6
> GROUP BY AdrGroup_Id HAVING COUNT(*)=1

No, when you say group_id = 6 then you have already the adresses which belongs to this
group and the result of the 'group by having' statement is always 1.  That's not the solution. Consider the following
situation. 
The user sent a letter to the persons which belongs to the adressgroups 1,2 and 3.
Now he like to send the same letter to the persons which belongs
to adressgroup 6. But It can be that  persons in group 6 belongs
either to group 1, 2 or 3. So the user wants only those adresses which belongs to adressgroup 6 and do not count to any
othergroup. That's way I tried  
the statement with the Exists clause.

Greetings Herbie

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Herbert Liechti                            E-Mail: Herbert.Liechti@thinx.ch
ThinX networked business services               Stahlrain 10, CH-5200 Brugg
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pgsql-general by date:

Previous
From: Bruce Momjian
Date:
Subject: Book completed through chapter 6
Next
From: Bruce Momjian
Date:
Subject: Publisher looking for Reviewers